Browsing as a guest. You can post with a name and email, like, and report. Sign in as Maya, Tomas, Priya… to pin, moderate, and see author badges from the other side.

Practical Eloquent · Chapter 4

Eager loading and the N+1 problem

Maya Chen · 12:23

  1. 01 Spotting N+1 in the query log
  2. 02 with() versus load()
  3. 03 Constraining eager loads
  4. 04 Counting without loading
  5. 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!

Sam Okafor Sam Okafor

Is there a rule of thumb for when a join beats eager loading? The lesson says "when you need to filter or sort by the related table" but I would love a concrete threshold.

Noor Haddad Noor Haddad

For anyone following along: Laravel 11 and up lets you limit an eager load natively, so $query->latest()->limit(10) inside with() works now without a package.

Lukas Novak Lukas Novak

At 7:20 you use withCount instead of loading the relation. I understand that it is faster, but I do not understand why — isn't it still a query per row?