Recap: Efficient WithCount0:03
We have spent a lot of time talking about eagerly loading data because it directly affects the performance of our application. Like for example, here we are displaying our list of users. This is where we just have the number of teams that a user is a member of. And if you'll remember, we used the with count method to do that. And if we take a look at telescope, we're going to see a very efficient set of queries.
Add Team Tooltip UI0:25
And if we take a look at telescope, we're going to see a very efficient set of queries. In fact, there's just one query for fetching the users and then the number of teams for each user. And it doesn't get any more efficient than that. But I want to change this. I want to not only display the number of teams, but as we hover over these numbers, I want to display the teams. The user is a member of.
to display the teams. The user is a member of. This of course means we will need to change our view. So here is the table cell where we display just the teens count. And I'm going to replace this. And you know, ultimately the HTML isn't that important. Kind of we, we do still have our table cell and has a class of group so that whenever we hover over this cell, it's going
and has a class of group so that whenever we hover over this cell, it's going to display this tool tip here. And we have this unordered list to where we want to display our team names. So let's go ahead and do that with a four each. User teams as teams. And then we just want to output a LI element, and the text is simply going to be the team name. Now we can take this a step further
and the text is simply going to be the team name. Now we can take this a step further and we can have a link that we could click on to go to the page of that team, but this is gonna be fine, so that now inside of the browser we can hover over the number of teams and we see those names. Great, we've added some great functionality here, but if we take a look at the queries, there's a lot more going on.
Expose N+1 Queries1:56
but if we take a look at the queries, there's a lot more going on. Now we still have that first query where we select all of the users and the number of teams for each user, but then as we iterate to over each user, we are lazy loading the team information for that user. So we see a query for each individual user to get the teams. And we only have 10 users. But you know, imagine if we have a hundred users or a thousand users.
But you know, imagine if we have a hundred users or a thousand users. The more users we iterate over, the more queries we are making because we are lazy loading that information. Now, lazy loading is a fantastic feature. There are times when we definitely do want to lazy load data, but in something like this, we definitely don't. So one of the things that we could do is just turn off lazy
Disable Lazy Loading2:41
but in something like this, we definitely don't. So one of the things that we could do is just turn off lazy loading to begin with because then that would force us to think about when we need to access data that we might need. So to turn off lazy loading, we can go to the app service provider and we can say, model prevent lazy loading. And that does exactly what its name implies. It's going to turn off lazy loading so that now
And that does exactly what its name implies. It's going to turn off lazy loading so that now whenever we try to display our list of users, we get an error because lazy loading is disabled. So in a way, this is great because this gives us the opportunity to really think about how we want to load our data. In our case, we know that we want to display the team information. So inside of our route, you know,
Fix With LoadMissing3:27
to display the team information. So inside of our route, you know, we can change this up just a little bit so that we have our users, we fetch the users with the count. We still want that count, but then we will simply compact with our users here. But we know that we can load any missing data that we might need. So we could say users load missing, and then we are working with the team's relationship.
So we could say users load missing, and then we are working with the team's relationship. So we could load that after the fact, in which case everything works like it did before. And there's technically nothing wrong with that. But let's say that we also want to include some of the other relationships. You know, maybe we want to display the tasks as well, in which case we would just have to pass in an array of all
Enable Relationship Auto-Loading4:14
You know, maybe we want to display the tasks as well, in which case we would just have to pass in an array of all of the relationships that we wanted to load. That's okay, and, and we've had to do that for a long time. But now we have something that's really impressive. It's called with relationship auto loading. It's a mouthful to say, but it works beautifully in that it is going to automatically load the relationships that we need. Now that's, that's important
to automatically load the relationships that we need. Now that's, that's important because it's not gonna load every relationship and all of the data for those relationships. No, it's only going to load the relationships that we use. So in the case of our user list here, we display the users naturally, but we also display the teams. So behind the scenes, eloquent is going to recognize that we want to access the team information.
So behind the scenes, eloquent is going to recognize that we want to access the team information. So it's going to eagerly load that information, and we can prove that by going to telescope. And we can look at our queries here. We have our first query to select to all of the users and of the number of teams for each user. But then here's the other query for fetching all of the team information and just to prove it to you
of the team information and just to prove it to you that it does it based upon the data that we use. Let's just take out this for each loop. And since we are no longer working with the actual team information, we can take a look at the queries and sure enough, that query to load the team information is no longer there, but we still have our main query of our users
to load the team information is no longer there, but we still have our main query of our users and the number of teams for each user. So really this is the perfect way of working with relational data. It eagerly loads just the data that we need to work with, but the downside, so we have to call this method or do we? No, we don't because we could set this at a global level, which you can make the argument that you wouldn't want to do
No, we don't because we could set this at a global level, which you can make the argument that you wouldn't want to do that in some cases, but for the most part, why not? The method is called automatically eager load relationships. Again, it's a mouthful, but this sets it at a global level. So now you can lazily load data, but it's done so in a very safe and efficient way, that data is eagerly loaded and it's only done so when you need to access that relational information.
and it's only done so when you need to access that relational information. So once again, we can see our list of teams as we hover over these numbers. And if we take a look at the queries while we have our main query, then we have the query to select all of the teams. And that's it. We still essentially lazy load our team data, but it is done so in a way that it is efficient and it's actually eagerly loaded. Now, I'm not gonna say that you should always enable the
and it's actually eagerly loaded. Now, I'm not gonna say that you should always enable the automatically eager load relationships feature, but really it's hard to think of a time when you don't want to. In fact, I would say that the vast majority of our applications would benefit from enabling that feature. It's a game changer really, and until I come across a situation where I don't want
It's a game changer really, and until I come across a situation where I don't want or need to enable it is probably going to be the very first thing that I do when I set up a new project.
