This episode is for members only

Sign up to access "Eloquent Relationships By Example" right now.

Get started
Already a member? Sign in to continue
Playing
25. Defining the inverse of the relationship

Episodes

0%
Your progress
  • Total: 4h 18m
  • Played: 0m
  • Remaining: 4h 18m
Join or sign in to track your progress

Transcript

00:00
So defining the inverse of the relationship sounds pretty boring but this opens up a whole
00:05
new level of functionality that we can add to what we're building. What I mean by this is what we can do in the URL is create something like topics slash and then laravel for example and now with this new URL once we have defined the inverse of the relationship we can get back all of the courses that exist under this particular topic. So let's go ahead
00:28
and define the inverse of the relationship and build out this example URL. So we know that over on our course we defined out that a course has multiple topics via this relationship type but now if we head over to our topic model this has many courses via that relationship. So let's go ahead and create out a courses relationship in here and we're going to return exactly the same
00:53
thing but of course we're going to relate this back to a course. So belongs to many and we're going to pass our course model in and that's pretty much it. It works exactly the same in either direction. Okay so let's build out an example root for this so we can actually see this in action. We're going to go ahead and say slash topics slash and then we'll grab the topic out here
01:17
via root model binding and we can actually rather than plug this out by the id use a colon here to plug this out by the slug so it's a little bit more friendly. Okay so let's create out the closure for this root of course this could go into a controller and we know that into here we're going to get the topic that is in the URL via root model binding. Okay let's just die dump on that topic
01:37
just to make sure this is all working and let's head over to slash topics slash laravel. Okay so we've got that topic in there and now what we can do is we can read all the courses from that particular topic. So we're going to go ahead and create out a new view in here so let's create out a topics folder and then let's just create show.blade.php and into here we can go ahead and
02:01
iterate through all the courses. So return view and this is going to be topics show and we're going to pass the topic down into here. Now that's all we need we don't necessarily need to extract all of the courses out in here because we can do that directly within our blade view although you can separate these if you want to by saying courses and then topic and courses but we'll do that in
02:26
our blade view and you can do that if you want to. Okay so now over in our show.blade.php file probably the first thing would be a good idea to do is just output the topic title just so the user can see what topic this relates to. That's the first step and we can see laravel and then underneath this we can go ahead and output each of the courses that belong to this topic. So for each and now we
02:50
need to say topic and courses as course and go ahead and end that for each loop in here. Now we can create our container that shows each of them courses. So we'll just say course and title let's head over and see what we get. Okay great so we've got eloquent relationships by example so let's go ahead and duplicate this over and we're going to head over to our home page. We want to add the
03:16
laravel topic to learn inertia as well. So let's just double check in here we can actually do this manually in the database because we already know how to attach this. So let's say learn inertia needs to be hooked up to laravel that now means that laravel under the topic should have two different courses and sure enough you can see that works. So again we have the inverse of the
03:38
relationship but now this allows us to somewhere in our app list all of the courses with all of the topics that are available for that course and now we can look what courses are under that particular topic. So it opens up that whole new level of functionality just with the same relationship type and just defining the inverse of that relationship. Now let's say for example
04:00
we were building out this entire page in reality these things would probably be the same as the home page. We would want to list out all of the topics for this particular course as well. So let's take this a step further just so we can see what would happen in reality. So I'm going to head over to the index page here where we're doing this and actually listing out all of our courses and in
04:22
actual fact we could grab the entire structure of this because it's going to look exactly the same as inside of here. So we're duplicating this we could create a blade component to make this a little bit better but let's keep this really simple for now. So I'm going to go ahead and just let's just make sure we indent this properly and yeah there we go. So we'll just pull all of this
04:42
out quick and there we go. So this should work because technically we're iterating through our courses we show the course title and then we show all of the topics for that particular course and let's just bump this to an h2 so we get the different sizes. Okay great so we've got a Laravel topic that contains eloquent relationships by example. We know it has the Laravel topic but it
05:04
also has the eloquent and inertia topic as well. Same for learn inertia and that has Laravel and view as their topics. So this is really good because now we have a list of our courses shows the topic shows the same information but we're just filtering by the topic but take a look at the query count. So we need to figure out how to egoload all of this stuff in so we don't run into an n plus one
05:27
problem. Again a really important part of working with eloquent. So how are we going to egoload in the topics for each of the courses contained within this topic? How do we do that? Well we can't egoload at this point because this is being fetched out by root model binding. There is a way to egoload here but it would be much better to use a egoload directly within this controller or root
05:50
to egoload after we've got the topic out. So we're going to say topic and then we're going to say load. So this is not with because we're not building up a query but we're saying load these after we've already got the topic in. So what do we want to egoload? Well egoloading in the course isn't going to have much effect because they're just a list of courses. If we give this a
06:09
refresh you can see the query count doesn't change at all. But what we do want to do is load the topics within the courses because they are going to be listed. How do we do that? There's a couple of ways but with this simple example the easiest way is to use dot notation to egoload a nested relationship inside a thing that you've already got. So we can load the topics within the courses
06:33
by doing this. So let's go over and see the difference. Okay so the query count has come down to three. That might not seem like a lot but now if we have more courses in here and more topics that is going to have an effect. So just as an example let's go ahead and attach the Laravel slug or Laravel topic to learn Laravel. Save that out and let's get rid of our egoloading
06:58
just temporarily and see how many queries we get. So we get five effectively fetching these for each of them. If we bring this back in that should bump us back down to three which was what we saw before. So this is perfectly acceptable. We have gone away from that n plus one problem. We're using one query to select all of the topics and then we are using two queries to select first of all all of the
07:21
courses and second of all all of the topics for all of the courses that we are listing. So not only have we defined the inverse of the relationship and built our practical example we've also come back to our eloquent egoloading issue with that n plus one problem and fix that up as well which is really really important.
33 episodes4 hrs 18 mins

Overview

Eloquent is Laravel's ORM (Object Relational Mapper). In simple terms, it's how your models work with the database.

The good news? There's a bunch of powerful relationship types available. Our task is to learn when and where to use each one.

In this course, we'll cover each basic relationship type, how to access related models, and then insert, sync, update and delete related data. Oh, and we'll build a practical example for each relationship type, to really make it stick.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.