Laravel

Laravel Eloquent: whereHas Deep Filtering

January 16, 2026 1 min read 14 views

Filter with whereHas:

Basic whereHas

$users = User::whereHas('posts', function ($query) {
    $query->where('is_published', true);
})->get();

With Count

$users = User::whereHas('posts', function ($query) {
    $query->where('is_published', true);
}, '>=', 5)->get();

Nested Relationships

$users = User::whereHas('posts.comments', function ($query) {
    $query->where('approved', true);
})->get();

Or whereHas

$users = User::whereHas('posts')
    ->orWhereHas('comments')
    ->get();
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!