PHP
Filter Models by Related Record Existence with Eloquent `has` and `doesntHave`
Learn to retrieve parent models based on the existence or absence of their related child records using Eloquent's `has` and `doesntHave` methods.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
// Get users who have at least one post
// $usersWithPosts = User::has('posts')->get();
// Get posts that have at least one comment and are published
// $postsWithComments = Post::has('comments')->where('published', true)->get();
// Get users who do NOT have any posts
// $usersWithoutPosts = User::doesntHave('posts')->get();
// Get posts that do NOT have any comments
// $postsWithoutComments = Post::doesntHave('comments')->get();
// Querying with whereHas for specific conditions on related models:
// Get users who have posts with 'active' status
// $usersWithActivePosts = User::whereHas('posts', function ($query) {
// $query->where('status', 'active');
// })->get();
How it works: The 'has' method allows you to retrieve parent models that have at least one related child record. Conversely, 'doesntHave' retrieves models that have no related records. For more complex conditions on the related model, 'whereHas' lets you add additional query constraints to the relationship query itself, providing powerful filtering capabilities without loading all related data into memory.