Laravel Eloquent: whereHas Deep Filtering
January 16, 2026
•
1 min read
•
15 views
Table of Contents
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();
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!