💡 Understanding with() vs whereHas() in Laravel Eloquent
💡 Understanding with() vs whereHas() in Laravel Eloquent
💡 Understanding with() vs whereHas() in Laravel Eloquent
with() — Eager Loading
$users = User::with('posts')->get();
whereHas() — Filtering Based on Relationship
$users = User::whereHas('posts', function ($q) {
$q->where('status', 'published');
})->get();
📌 Key Difference
with() = data loading optimization
whereHas() = data filtering logic
User::with('posts')
->whereHas('posts', function ($q) {
$q->where('status', 'published');
})
->get();