Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Model relation is how we tell out Todo Lists, how to find out Todo Items for each list.
-
0:00
[MUSIC]
-
0:04
What good is a to do list without to do items.
-
0:07
Without a doubt, we're gonna wanna keep our two
-
0:08
types of data separate, but relate them to one another.
-
0:12
Eloquent provides us a simple and effective way to relate our two models
-
0:16
as well as giving us flexible access to those relations through custom methods.
-
0:21
The relation of our data and the way we chart a path to
-
0:23
that data is where the power of Laravel will begin to show through.
-
0:28
We have successfully walked through creating, reading,
-
0:32
updating, and deleting our to do list.
-
0:34
So now we have our to do list, but the main
-
0:37
part about to do list is not even close to complete.
-
0:40
Which if we go to My Listings, being our first list here,.
-
0:44
Let's look where are our items.
-
0:46
We need items to actually complete.
-
0:49
So the next step for us is to create a new
-
0:51
model and relate that model to our current to do list model.
-
0:56
We're going to call this next model to do items.
-
0:59
So, the first step for us is to create our new model.
-
1:02
We're going to do that the same way we've done before by heading into our
-
1:06
vagrant, and using an artisan command to create
-
1:10
a new migration to create a new model.
-
1:12
Let's do that.
-
1:14
So here we are inside of our vagrant instance.
-
1:17
We can do a PHP artisan and then migrate and then make.
-
1:24
We're gonna have a name to the migration, makes perfect sense, so let's go very
-
1:30
verbose and say create_todo_items_table, so we now are
-
1:42
gonna create a table but we need to pass the create
-
1:45
flag because we don't have a table and we're actually gonna call
-
1:49
the table to do items.
-
1:54
So we have to do lists and now we have
-
1:56
to do items, so we're gonna hit enter on this.
-
1:59
So it certainly helps if you spell things correctly.
-
2:01
That is definitely not how you spell my grade.
-
2:04
I'm gonna hit up to get my history, go back over here and fix my spelling.
-
2:11
All right, let's try that again.
-
2:13
Okay so we created our migration, we clear our screen and head back over to
-
2:18
the sublime text, head into our database and look at our second migration.
-
2:24
So here's our migration, we're creating a to do items table.
-
2:29
We're adding our timestamps.
-
2:30
We are adding our ID for increments.
-
2:33
So, what are we gonna do, what do we need here?
-
2:36
Well, we're definitely going to need our item name so we'll add that in.
-
2:39
But we're also gonna need to wait to relate what item
-
2:43
belongs to a particular list, and that's where we get into relations.
-
2:49
So, let's start by adding our string in that's going to give us our name.
-
2:53
So go in, use table and then we're gonna
-
2:56
do a string again, and we're gonna call this content.
-
3:02
That'll be our to do list content.
-
3:04
We also want our to do list content to be unique,
-
3:07
so let's do a unique, and then we'll close that one out.
-
3:12
So now we're gonna add at the very least a string for content.
-
3:15
We've got our auto incrementing ID and our
-
3:18
timestamps to get are created on and updated at.
-
3:21
The other thing that we're gonna need here is to know when we've completed this.
-
3:25
It's super necessary but it could be very useful to us in the future.
-
3:29
Let's go ahead and add that now.
-
3:30
So table, and then we're gonna add a new type,
-
3:34
one that you haven't seen yet, called the dateTime, okay.
-
3:38
And then our dateTime we're gonna call completed_on [SOUND] all right.
-
3:44
And it's allowed to be null because it's not gonna be completed to start with
-
3:48
so we're gonna let it be a null value so here we're gonna use, nullable.
-
3:56
Close that out.
-
3:57
So, before we complete our migration we're gonna want
-
4:01
to have some sort of related table field here.
-
4:05
So, let's take a look at the migrations and see which one is going to work for us.
-
4:10
Let's head back over to the docs and do that now.
-
4:12
So, under the Laravel docs, inside of
-
4:14
the eloquent section, there is heading called relationships.
-
4:18
There's several relationships to describe here, one to one,
-
4:22
one to many, many to many, has many through polymorphic.
-
4:26
It can get really complicated.
-
4:28
For us, it's very very simple.
-
4:30
The item itself is definitely going to belong to a list.
-
4:35
But a list is going to be able to have many items.
-
4:39
So it's definitely going to be a one to many relationship.
-
4:44
So we click on one to many, and read what it says.
-
4:47
An example of a one to many relationship is a blog post.
-
4:50
So a blog post could have many comments.
-
4:52
So this is how we deal with the model relation.
-
4:55
So, here you see in our model for the post, and it's extension of eloquent.
-
5:01
We're going to have a function the called comments that will
-
5:04
return has many, and then the actual model which is comment.
-
5:08
So for us, let's switch over to our model.
-
5:11
For the actual, to do list and add in our list items.
-
5:16
So here, we're going to go in to our models and look at our to do list.
-
5:21
So here's our to do list.
-
5:22
We're extending eloquent and in here we're
-
5:25
going to create a new public function, right.
-
5:30
And our public function is going to be how we're going to get the item.
-
5:34
So we're gonna have list items.
-
5:37
So here are our list of items.
-
5:39
All right.
-
5:39
And then we'll go down close this out and then we're gonna put return this, to
-
5:47
access the current instance of the model or
-
5:50
this instance of TodoList and then hasMany and
-
5:56
it's going to have many to do items, so
-
5:59
we'll just say TodoItem, which TodoItem is our model.
-
6:03
So we're gonna need to go ahead and create our model now.
-
6:06
So same thing as TodoList except we're gonna do new file, PHP, actually save it.
-
6:12
And call it TodoItem.php.
-
6:19
Same thing as TodoList, so we're gonna actually just copy
-
6:21
all of this here, go into TodoItem, paste it in.
-
6:25
So everything here is right except TodoItem will be our name.
-
6:30
It's definitely gonna extend eloquent.
-
6:32
It's not gonna have ListItems.
-
6:34
It's actually gonna have a todoList.
-
6:37
All right, and then it's gonna return not hasMany, but it belongsTo
-
6:44
a not TodoItem, but TodoList.
-
6:52
Okay.
-
6:52
So, here we have our two models, the TodoList hasMany Todotems under ListItems.
-
6:58
And then we have a TodoItem which belongs to one TodoList.
-
7:03
So now, we've actually related our models.
-
7:06
Let's go back to the documentation and see what else we need to do.
-
7:09
So underneath Relationships, if we click on the One To One, it's gonna give
-
7:12
you a little bit of an explanation
-
7:13
here about how SQL performs the actual statement.
-
7:17
So here we're looking for a user where the ID is one.
-
7:21
So that's finding a user.
-
7:22
But then here, we have a relation where it has one phone, so
-
7:26
now it's saying, select from phones where the user_id is equal to one.
-
7:30
Here it's saying that it's assuming that there is a foreign key, that's
-
7:34
the key point here, foreign_key, that the relationship is based on the model name.
-
7:39
So in the case of phone, a phone will assume to have a user_id.
-
7:44
So in our case, if we head back over to our schema here,
-
7:49
we're gonna make sure that this has a not todo_items ID, but a todo_list ID.
-
7:55
We're gonna add that by going directly under the ID
-
7:57
here and doing table and then pointing to an integer.
-
8:05
Which, we want an ID, which is an integer, but we don't want it to auto increment and
-
8:08
the name of that field or name of that column is going to be todo_list ID.
-
8:15
So it's based off of our model name to do list, and then ID.
-
8:19
Close that out, and then we're going to go ahead and run our migration.
-
8:22
Notice that if we do a down, or go reverse in our
-
8:25
migration, it's just going to drop the entire to do IM's table.
-
8:29
Let's switch over to the terminal and run our migration.
-
8:32
So, php artisan migrate.
-
8:38
Okay, let's run our migration.
-
8:40
Let's look at our database, refresh, get rid of all the excess and
-
8:44
now if we refresh our tables, you'll see we now have to do items.
-
8:49
Nothing in here, but here are our columns, ID, to do
-
8:52
list ID, our content, the completed on, and then our typical timestamp.
-
8:56
So migration ran great.
-
8:58
Here's our to do list.
-
8:59
Now we just need to start by adding some items.
You need to sign up for Treehouse in order to download course files.
Sign up