Laravel theorytheory 0/50 · 0%
Eloquent · easy

12. Eager Loading

Avoiding the N+1 query problem.

Lazily accessing relationships in a loop triggers one query per iteration, known as the N+1 problem. Eager loading with ->with('relation') fetches related models in a single additional query. Nested and conditional eager loading are supported via dot notation and closures, e.g. ->with(['comments' => fn($q) => $q->latest()]).

Check your understanding

  1. 1. What problem does eager loading solve?

  2. 2. Which method eagerly loads a 'comments' relationship?