Explain the 'N+1 Query Problem' in Eloquent or Doctrine and how 'Eager Loading' resolves it conceptually.

Last updated 15 June 2026

  • N+1 happens when 1 query loads parent rows, then accessing a relation in a loop fires 1 extra query per parent (e.g. 100 posts = 101 queries).

  • Eager loading collects parent IDs and fetches all related rows in one WHERE id IN (...) query, so the total is 2 queries regardless of row count.

  • Eloquent uses Post::with('author'). Doctrine uses DQL JOIN FETCH or fetch-mode EAGER.

  • https://laravel.com/framework/docs/12.x/eloquent-relationships
  • https://msaied.com/articles/eloquent-n1-at-scale-eager-loading-strategies-subquery-selects-and-lazy-eager-loading
  • https://dev.to/vimuth7/eager-loading-n1-issue-and-query-optimization-in-laravel-5555
  • https://medium.com/@jonthanLight/understanding-and-resolving-the-n-1-problem-with-doctrine-in-symfony-cfef1d09af6f
  • https://medium.com/@abdullahbinshahid/eloquent-n-1-problem-with-vs-load-vs-has-explained-b336b8688741
  • https://medium.com/@chinthakahasan/lazy-loading-vs-eager-loading-in-laravel-understanding-the-n-1-query-problem-64036792d82a
  • https://dev.to/mattleads/stop-n1-queries-forever-advanced-doctrine-orm-strategies-in-symfony-81-b8b
All PHP interview questions