This course will be retired on January 24, 2020.
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
Accessing our Relationships
-
0:00
Now we need to find a way to access our relationships.
-
0:04
So, we've related the two models, we've created our database for the to do items,
-
0:10
we've given it a foreign field or a foreign key for the to do list ID.
-
0:15
Now it's time to look at our listings, so here you
-
0:18
see my listings or my list and we need some items.
-
0:23
Before I do that, I'm gonna clean up my list
-
0:24
titles to make it seem a little more sensible, so
-
0:28
let's head back to our main listing here, our main
-
0:30
page and I'm gonna edit my lists that are currently there.
-
0:34
So I'm going to destroy this list and then I'm going to edit this one
-
0:39
and call it Hampton's tasks, and hit update,
-
0:46
and then I'm going to edit this one, and do Charlies tasks.
-
0:55
And then say update that one.
-
0:57
Ok, so now we have two people, two task lists.
-
1:01
So, let's go to my tasks and I have none.
-
1:04
So I want to create some tasks.
-
1:06
Let's do it manually.
-
1:07
Use our related ID and then use our relationship in a
-
1:11
loop to get our data and display it to the screen.
-
1:14
Let's start by adding some manual data to our database.
-
1:18
So, first, my to do list is ID of one, Charlie's is to do list ID of sixteen.
-
1:24
So keep that in mind as we're adding items to each person's list.
-
1:28
Let's add a couple to mine.
-
1:29
So, the first ID is going to be automatic.
-
1:33
The to do list ID for me is one and, let's say, wash
-
1:38
car and then don't need it completed on, but we'll do it now, as our date.
-
1:46
Same thing here and now we have one item.
-
1:50
Let's go ahead and add a couple of more items.
-
1:53
So, automatic one, and then, clean house.
-
1:58
That's a big item.
-
2:00
And then, now and now.
-
2:02
Okay.
-
2:03
So here I have two items.
-
2:05
Let's go ahead and refresh our page.
-
2:07
Nothing is going to be here cuz we need to find a query.
-
2:10
We'll do that inside of our controller and dump it back to the
-
2:13
screen so we can actually show my items that need to be completed.
-
2:16
Head back to our controller.
-
2:18
Go to controllers, TodoListController, and then scroll down and we're gonna use the
-
2:24
show method here on the TodoListController to show our to do items.
-
2:31
Now, a to do item should need its own controller, and it will, but for us right
-
2:36
now, we want to show our to do list items on our to do list page or show page.
-
2:42
So we're going to actually do our query right here.
-
2:45
Now, how are we going to do that?
-
2:46
We have our list which is good but now we need a list of our items.
-
2:50
So let's create a, another variable here which will end up
-
2:55
being an object, items and then we're gonna do another search.
-
2:59
We're gonna search on our existing found to do list.
-
3:04
So, that is list, which has been found previously or failed and then
-
3:08
we're going to pass through our argument or our find for the related items.
-
3:14
How do we know what that's called?
-
3:15
Well, we defined it earlier on the to do list model.
-
3:19
List here, this object, is an instance of the to do list model.
-
3:23
So, if we open up the to do list model and
-
3:27
we look, we only have one method here and that's list items.
-
3:31
So it would make sense to get the return of
-
3:33
all these to do items, we're gonna say list items.
-
3:37
So let's head back over to our controller
-
3:38
and say, list items, right here under show.
-
3:42
Then open and close parents and then we want to do a query on those
-
3:47
which is get and that's gonna return all of them to us in this list.
-
3:51
Let's go ahead and return the item to the screen.
-
3:55
Actually, we'll be items plural, so let's return the items to the screen.
-
4:02
See what we get.
-
4:04
Refresh and here are my items.
-
4:06
If you notice, I have todo_list_id, Wash Car and Clean House.
-
4:11
Doesn't have completed on but it does have our created and updated_at.
-
4:14
So it's returning our records and it's done it very simply
-
4:17
just by passing through to do list, list items and then get.
-
4:22
It's super simple but what's going on the background is not.
-
4:26
Let's add a little something to our routes file, so you can kind
-
4:29
of see what's happening in the background as we continue going through this process.
-
4:33
Let's open up our routes file.
-
4:36
Now, in our routes file, we have a route resource and we have
-
4:39
a route get, but Laravel has something even cooler than that, called an event.
-
4:44
So event, which is any event, and we're gonna listen, so a listener.
-
4:49
Listen and we're gonna listen for something in particular and for us it's
-
4:53
gonna be the illuminate, but .query.
-
5:01
So this is listening for any query event that comes across the illuminate class.
-
5:06
So the main core and then we're going to pass through a closure or a function, that
-
5:13
has a single argument that comes through and that's gonna be the actual query.
-
5:18
All right?
-
5:19
Now we're gonna open up and close this, just like we've
-
5:23
done before in closures, but we're gonna return something back here.
-
5:26
We're just gonna var dump.
-
5:27
And we're gonna var dump our query.
-
5:34
Let's go back over to our controller and then inside
-
5:36
of our controller we're going to take our return items here.
-
5:38
Let's go ahead and get rid of that and just go
-
5:40
back to what we had before which is just showing the list.
-
5:42
We know we're collecting the items so we're definitely going to be running
-
5:45
some queries, we're just not passing them back through to the view just yet.
-
5:49
But let's see what queries are actually running.
-
5:55
So here as we've refreshed our page, you can
-
5:57
see that here's the list that says: Hamptons Tasks.
-
6:00
There are no task listed, but you can see the two queries that ran.
-
6:04
We have selected all from to do lists, where the id is equal to this ID.
-
6:09
So there's one, limit is one and then we are selecting all from to do items which
-
6:14
is our new table, where, to do items dot to do list ID is equal to the same.
-
6:21
So, you see, we're using a related query, but we've done it with eloquent, and we've
-
6:26
done it very, very simply, just by saying list, list items, get.
-
6:31
The next thing we're gonna wanna do is actually take
-
6:33
these items and pass them through to our show page.
-
6:38
So, I'm gonna tab this down, just for readability
-
6:40
and say, with again, so yet another chain, with
-
6:45
items, and then we're gonna pass through our items
-
6:47
object here, which is gonna have all of our items.
-
6:51
Then we need to switch over to our view to display them.
-
6:55
We're gonna go to our to do's and then show and
-
6:58
then run a for loop here underneath each of these names.
-
7:04
Right, and then show what our items are.
-
7:07
So let's go ahead and do that now.
-
7:09
We'll do it with a simple for each inside of a blank syntax.
-
7:12
So at for each, and then inside of the for each, we're going to do
-
7:18
what we need and then to close to our four each, we're going to say at, end for each.
-
7:24
And inside of here we wanna list all of our items,
-
7:27
but we have to pass through what we want as our arguments.
-
7:30
So, for each items, which we know what's coming through, as item.
-
7:37
Okay, so now we'll do a list of the items.
-
7:41
So, we'll just do it as h4's.
-
7:47
Our triple curly brace syntax, some spaces in there for clarity and then
-
7:53
do item and then we want to get the item content which is the name of our field.
-
8:00
So save that, let's go back to our page and refresh.
-
8:03
Now we have a list of my items, which are wash car and clean house.
-
8:08
Notice we've still got the queries up there, so we can for now go ahead and just
-
8:12
comment out that section instead of our routes,
-
8:14
so we don't need to see those right now.
-
8:16
We will want to readdress those later, but we'll
-
8:19
do that in our final stage where we're refactoring.
-
8:21
Head back over to Sublime, we can close this, close out all of this and
-
8:26
head to our routes, and then we're just going to comment out these three lines.
-
8:30
So now I'll go back to our page and refresh.
-
8:33
Now we have our items, so here's my task, and here are my items.
-
8:36
Just to make sure that we're doing the right
-
8:38
queries all the way across the board, let's add a
-
8:41
new item to our database, and that's going to
-
8:43
be an item that's related to Charlie's tasks, not mine.
-
8:46
So we're gonna make sure that it has the related ID of 16.
-
8:50
So, let's do another one.
-
8:53
So, new item.
-
8:54
Related ID is 16, or to do list ID and
-
8:57
then this one will be, eat all of your dinner.
-
9:03
All right, and then now,
-
9:08
do the same thing over here, and hit enter.
-
9:11
So, eat all of you dinner, let's let's update that, eat all of your dinner.
-
9:17
All right, now head back over, refresh my page.
-
9:22
I don't have that item, but if I go back to the main page and click on her tasks.
-
9:27
Eat all of your dinner is her tasks.
-
9:29
So very simple, little bits of code just a couple of
-
9:32
lines and eloquent has really done some powerful things in the background.
-
9:36
The next step for us to do is to
-
9:38
continue our [UNKNOWN] by nesting our routes, nesting controllers,
-
9:42
and making all of the stuff work the same as to do list, but just with our items.
-
9:48
Over the last several lessons, you've absorbed a lot of information.
-
9:52
My first suggestion would be to start from
-
9:54
the beginning and go through the process again.
-
9:57
The second and third time around, you will not need to focus on the
-
10:00
tests and the challenges, but only on the task of building out the application.
-
10:05
Please reach out on our forums, and get involved in the Lauravale community.
-
10:09
Together, we can grow our knowledge.
-
10:11
Until next time, remember, work hard, and have a little joy.
You need to sign up for Treehouse in order to download course files.
Sign up