9 hours 41 minutes
I Welcome back to module four. This is less than 4.5 implement model relationships. One
and this. Listen, we're gonna be implementing some relationships on our level Models level supports all the primary relationship types such as 12111 Too many, many to many. And polymorphic, we're gonna be using the polymorphic one. Hauer.
This is going to assist us with database seeding and again that is simply adding a bunch of fake data to the database so that we can easily test our database and develop queries against it.
We won't be using other features available on level models. Most of these deal with making Web design easier.
And in addition, lucky for us level makes the syntax for adding these types of relationships pretty easy. For example, in the phone example at the bottom, we can see that
we used the key word. This has one app, backslash phone. And that's pretty much how all the relationships work Now with that said, let's go ahead and get started.
Okay, here we are. It's a good idea to look at the e r D diagram as we work through this. If we look at this diagram, we can see that a user has many grades. A course has many grades. A user has many user course and, of course, has many user course. And a user type has been a user course.
Now the thing to note here, too, is that user underscore courses, really a pivot table.
We could say that a user could have many courses and, of course, can have many users, so that would be a many to many. Larible does facilitate that relationship, but there are extra
caveats to that. There's things you have to do to facilitate that relationship. We aren't going to do that. We're just going to implement each as its own model and use that in the seating of our database. So we're gonna keep things a little simple for our task, and that's just we're going to just put in a bunch of has many relationships on our models.
So with that said, let's go ahead and get started.
We'll start with the user,
so I'm gonna and these files are located in your shared
So I'm gonna open up user
and I'm going to paste an example from the level documentation.
So this example is not correct.
And instead, I'm gonna call this one.
has many up
like that. So we're using the singular form here,
and we have one more we need to add, which is the user course.
So we're gonna say,
Well, this one
actually user courses
Next, let's go ahead and
at in the grade.
Now, this one isn't one too many. This is a belongs to
So this is the reverse of the one to many.
And I'm again. I'm Hasting example from the level docks. But that is this isn't correct. So lets a user this great belongs to user.
I'm a chance this to user,
And same thing it belongs to, of course.
And save that
on next, I'm going to go ahead and jump down to user course.
This is gonna have a bunch of belongs to.
This belongs to a user,
so that one's correct
also belongs to a course,
and it also belongs to a user type.
Now, when we're in, the lateral framework is you may have noticed we're not putting an underscore in between these words,
Uh, as there is in the database.
So let's go ahead and go back to course
that this was gonna have a belongs to
and it's gonna belong to a room. So I'm gonna save room.
This belongs to APP,
and it's also gonna have it has many user course,
so user courses,
and also has many grades.
We need to be sure to change this and used the right syntax. So has many
spell it, right,
So that finishes that one
on. Let's open the user type and get that one finished
on. This was gonna have many user course.
That looks correct.
Now we need the room.
So this isn't user courses, but it is courses
And we will be testing these relationships to make sure we have them correct using tinker in the next couple of lessons.
So room can also belong to a
and a spill has many, right?
Sure, I got that right in the last couple.
so room has a belongs to
belongs to building,
and then that just leaves building. So let's go ahead and open that one up.
And this has many rooms, so building can have many rooms that makes sense.
So this has many
so rooms. Here we go,
and that should handle all the relationships for our tables.
So let's go ahead and head over to this summer and discuss what we did in this lesson.
And that brings us to the 4.5 implement model summary. So what did we do in this lesson? We implemented some model relationships. We discussed why we're using the models. This is primarily to make the database seating easier. It will be easier to respect the constraints in the database because the constraints are gonna require that certain things exist before we add
other records. For example,
we can't add a user to a course if that user does not exist.
So that completes this lesson. And I hope to see you in the next thank you.
The Microsoft Azure Test is a premium Cybrary assessment created by Interview Mocha. The exam ...
AWS Data Engineering
The AWS Data Engineering test is a premium Cybrary assessment test created by Interview Mocha. ...