Eager Loading
Also Known As
eager loading ORM
with() loading
JOIN preloading
TL;DR
Loading related data upfront in a single query rather than deferring until access, preventing N+1 query problems.
Explanation
Eager loading fetches all needed data in one query (or a small fixed number), whereas lazy loading issues additional queries when related data is accessed. In PHP ORMs, eager loading is typically triggered with with() or join() methods. It is the remedy for N+1 query problems: instead of loading 100 posts and then issuing 100 separate queries for each post's author, eager loading fetches all authors in one additional query. The trade-off is fetching data that may not be needed — profile queries to find the right balance.
Common Misconception
✗ Eager loading is always better than lazy loading. Eager loading loads all related data upfront — useful when you know you will need it, wasteful when you load 1000 posts with their authors but only display 5. The right choice depends on access patterns.
Why It Matters
Eager loading fetches related data in a single query upfront — eliminating the N+1 problem where displaying a list of records triggers one query per record for related data.
Common Mistakes
- Eager loading every relationship by default — loading unused related data wastes memory and query time.
- Not checking that eager loading is actually used — an eager-loaded relationship that's never accessed is pure waste.
- Eager loading deeply nested relationships unnecessarily — each level multiplies the data fetched.
- Using eager loading in APIs where the client only needs IDs — load just what the response requires.
Code Examples
✗ Vulnerable
// Triggers N+1 queries — one per order
\$orders = Order::all();
foreach (\$orders as \$order) {
echo \$order->user->name; // each access fires a new query
}
✓ Fixed
// Eager load — single JOIN query fetches all related records
\$orders = Order::with('user')->get();
foreach (\$orders as \$order) {
echo \$order->user->name; // no additional query
}
// Nested relationships:
\$orders = Order::with(['user', 'items.product', 'shipment'])->get();
// Constrained eager loading:
\$users = User::with(['orders' => fn(\$q) => \$q->where('status', 'paid')->latest()])->get();
// Check for N+1 in development:
// Laravel Debugbar or:
DB::listen(fn(\$q) => logger(\$q->sql)); // logs every query
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Perplexity 8
Amazonbot 6
Ahrefs 3
Unknown AI 2
Majestic 1
Google 1
Also referenced
How they use it
crawler 20
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Low
⚡ Quick Fix
Add ->with(['relation1','relation2']) to your Eloquent query or JOIN in raw SQL when you know you'll need related data
📦 Applies To
PHP 5.0+
web
cli
queue-worker
laravel
doctrine
eloquent
🔗 Prerequisites
🔍 Detection Hints
Loop with $model->relation access without prior ->with() eager load; N+1 shown in Debugbar
Auto-detectable:
✓ Yes
laravel-debugbar
clockwork
laravel-telescope
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: File
Tests: Update