Dashboard eager loading setup0:01
So, this one's pretty interesting. I have a Laravel 10 application here, and on the dashboard, I'm loading Users that have blog posts. I'm eager loading those blog posts, and then I'll paginate the Users in sets of five. And you can see that on the front end. Here are the Users. Here's one. Here's another. And we have five Users, and then pagination at the bottom. And under each User, we output all the blog posts. We don't want to see all of a User's blog posts.
Limit-per-user eager loading fails0:29
And under each User, we output all the blog posts. We don't want to see all of a user's blog posts. We might only want to see the latest five blog posts for each User. So, you might think you can do something like this. We'll use a race syntax for the eager loading. We'll have a closure, accept a query, and then we could say, well, we want to update this query to grab the posts in latest order and limit them to just five per User. But when you reload, what you'll actually find is it only grabs five posts for every User across the board. In other words, we only see the first five posts for Emil, but we don't see Tejas, Ricks, Vincents, or Ivans. This is due to limits in SQL, and historically, you've had to install a separate package.
Laravel 11 fixes eager limits1:08
In other words, we only see the first five posts for Emil, but we don't see Tejas, Ricks, Vincents, or Ivans. This is due to limits in SQL, and historically, you've had to install a separate package if you want to get around this constraint in Eloquent. But in Laravel 11, all that changes. In our Laravel 11 application, let's introduce the same array syntax for posts. We'll accept the query once again, and, well, we'll grab them in the latest order and limit to the top five. And if we jump to the front end, well, it's exactly as you'd expect. We have all the users paginated nicely with their posts eager loaded, and each user has a five post limit. Of course, another way to achieve the same thing would be to introduce a new relationship on the User model itself. Perhaps we have a second hasMany.
Extracting to latestPosts relation1:53
Of course, another way to achieve the same thing would be to introduce a new relationship on the User model itself. Perhaps we have a second hasMany. This is latestPosts, and in the hasMany relationship, well, yeah, we're going to load Post class, but we're going to grab it in latest order, and we'll introduce that limit of five as before. Now we can go back to web.php. We can replace this custom array with latestPosts, and on the front end, which is dashboard.blade.php, we'll simply have to change for each User posts for each User latestPosts. If we do that, well, we have the exact same output on the front end, but we've extracted that functionality to the model. Either way, this was really annoying in previous versions of Laravel.
Wrap-up and takeaway2:34
but we've extracted that functionality to the model. Either way, this was really annoying in previous versions of Laravel. It's now fixed, and you don't have to worry about it again.
