Practical Eloquent · Chapter 4
Eager loading and the N+1 problem
Maya Chen · 12:23
▶
- 01 Spotting N+1 in the query log
- 02 with() versus load()
- 03 Constraining eager loads
- 04 Counting without loading
- 05 When a join is the right answer
Why your index page fires 200 queries, how to see it, and the three fixes in order of preference.
Student questions (3)
Or Log in to comment!
Is there a rule of thumb for when a
joinbeats eager loading? The lesson says "when you need to filter or sort by the related table" but I would love a concrete threshold.For anyone following along: Laravel 11 and up lets you limit an eager load natively, so
$query->latest()->limit(10)insidewith()works now without a package.At 7:20 you use
withCountinstead of loading the relation. I understand that it is faster, but I do not understand why — isn't it still a query per row?