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
24. Eager loading

Episodes

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

Transcript

00:00
We've already covered egoloading in the course, but it's worth covering specifically for this because we do need to add this in here.
00:06
So we're going to go ahead and pull in Laravar debug bar again. So let's go ahead and pull this into our project and we'll go ahead and attach some topics and see what we get here. Now, if we head over without actually outputting any topics at the moment, sure enough, if we look at our queries here, we have got three queries. Let's just add some more topics to these or add some topics to these and see what happens.
00:29
So we're going to head over to courses and one topics and let's attach as many as we can here. Should really be redirecting back here, but let's leave it and we'll do the same for course two as well. So we'll attach just view to that just to keep things simple. OK, so if we head over to our home page, you can see here that we've got three queries.
00:48
Now, this is the same as before, but the same thing was happening behind the scenes before we even had any topics attached to these. Notice we've got three queries. The first one is to actually pull out the courses. These two queries are duplicated technically because what they are doing is trying to look for the topics that relate to each of these.
01:07
If we had a third course in here, you would see that the queries would be bumped up. So let's go ahead and just add in a third course in here and just call this learn Laravel without even adding any topics to this. It's going to perform an SQL query to try and fetch topics out here. So we end up with four queries.
01:26
Again, we have run into an N plus one problem and we need to go ahead and eager load in this data. So let's head over to our web routes where we're actually outputting all of our topics just in here and our courses. And let's figure out what we need to do. Again, it's very simple at this point because we just have one relationship.
01:44
Nothing is nested here. We just need to say course with and then topics. And that's going to go ahead and eager load them in for us, despite the fact we're working with a slightly more advanced relationship. So there we go.
01:56
We've gone ahead and selected all of the topics that we need for each of the courses that we are working with. And there we go. We have all of our eager loading done. However many courses we add, however many topics we attach to each of these courses, we're only going to end up now with two queries.
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.