Implement Model Relationships Part 1

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10
Video Transcription
00:00
I Welcome back to module four. This is less than 4.5 implement model relationships. One
00:07
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.
00:22
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.
00:34
We won't be using other features available on level models. Most of these deal with making Web design easier.
00:40
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
00:51
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.
01:03
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.
01:18
Now the thing to note here, too, is that user underscore courses, really a pivot table.
01:23
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
01:34
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.
01:53
So with that said, let's go ahead and get started.
01:55
We'll start with the user,
01:57
so I'm gonna and these files are located in your shared
02:01
backslash
02:02
APP folder.
02:06
So I'm gonna open up user
02:09
and I'm going to paste an example from the level documentation.
02:15
So this example is not correct.
02:17
And instead, I'm gonna call this one.
02:21
Grades
02:23
has many up
02:25
backslash
02:28
grade
02:29
grade
02:30
like that. So we're using the singular form here,
02:37
and we have one more we need to add, which is the user course.
02:44
So we're gonna say,
02:46
Well, this one
02:49
courses
02:52
actually user courses
03:04
like this.
03:12
Next, let's go ahead and
03:15
at in the grade.
03:20
Now, this one isn't one too many. This is a belongs to
03:25
So this is the reverse of the one to many.
03:29
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.
03:38
I'm a chance this to user,
03:40
okay?
03:43
And same thing it belongs to, of course.
03:54
And save that
03:59
on next, I'm going to go ahead and jump down to user course.
04:04
This is gonna have a bunch of belongs to.
04:12
This belongs to a user,
04:15
so that one's correct
04:16
also belongs to a course,
04:27
and it also belongs to a user type.
04:36
Now, when we're in, the lateral framework is you may have noticed we're not putting an underscore in between these words,
04:43
Uh, as there is in the database.
04:47
So let's go ahead and go back to course
04:56
that this was gonna have a belongs to
05:00
and it's gonna belong to a room. So I'm gonna save room.
05:05
This belongs to APP,
05:13
and it's also gonna have it has many user course,
05:18
so user courses,
05:28
okay,
05:31
and also has many grades.
05:44
We need to be sure to change this and used the right syntax. So has many
06:00
spell it, right,
06:11
So that finishes that one
06:21
on. Let's open the user type and get that one finished
06:26
user type
06:29
on. This was gonna have many user course.
06:33
That looks correct.
06:36
Now we need the room.
06:48
So this isn't user courses, but it is courses
06:55
I see.
06:58
Change that.
07:01
And we will be testing these relationships to make sure we have them correct using tinker in the next couple of lessons.
07:09
So room can also belong to a
07:13
oops
07:15
and a spill has many, right?
07:18
Sure, I got that right in the last couple.
07:23
All right,
07:25
so room has a belongs to
07:28
a copy,
07:30
belongs to building,
07:46
and then that just leaves building. So let's go ahead and open that one up.
07:54
And this has many rooms, so building can have many rooms that makes sense.
08:00
So this has many
08:05
through,
08:11
so rooms. Here we go,
08:16
and that should handle all the relationships for our tables.
08:22
So let's go ahead and head over to this summer and discuss what we did in this lesson.
08:28
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
08:48
other records. For example,
08:50
we can't add a user to a course if that user does not exist.
08:54
So that completes this lesson. And I hope to see you in the next thank you.
Up Next