Video Version of the Lesson
[Only for premium members]
[Only for premium members]
[Only for premium members]
In the previous lessons, we discussed how to solve N+1 query problems using eager loading with the with()
method directly in your query. However, there are situations where you might need alternative approaches to eager loading. This lesson covers those techniques.
Let's return to our original example of viewing posts with their authors. As we've seen, without eager loading, this produces 31 queries - one for posts and 30 separate queries for users.
Sometimes you might receive an Eloquent collection from another part of your application, perhaps from code written by another developer, and you don't have direct access to modify the original query. In these situations, you can load relationships dynamically after...