Use withWhereHas() to filter records based on relationship conditions and eager load the filtered relationship in a single method.
Filtering models by relationship criteria while also eager loading the relationship previously required duplicating closures across whereHas() and with(). withWhereHas() performs both tasks in one call.
use App\Models\User;
// BEFORE: Duplicated relationship closure constraint
$users = User::whereHas('posts', fn ($q) => $q->where('published', true))
->with(['posts' => fn ($q) => $q->where('published', true)])
->get();
// AFTER: Combines filtering and eager loading cleanly
$users = User::withWhereHas('posts', fn ($q) => $q->where('published', true))->get();
- Eliminates duplicate constraint closures across whereHas and with
- Filters parent models while simultaneously eager loading matching children
- Keeps Eloquent builder queries concise and maintainable
Related Tips
View all tips →Stream Large Datasets: cursor() vs lazy() in Eloquent
cursor() hydrates single model instances sequentially using database cursors; lazy() streams records in chunks backed by LazyCollection.
Choose the Right Processing Method: chunk(), lazy(), chunkById(), lazyById()
Understand offset vs keyset pagination when processing large datasets to avoid missing records during updates.