Laravel Tutorials

Simplify Negative Relation Queries with Laravel's whereDoesntHaveRelation Methods

Published
Simplify Negative Relation Queries with Laravel's whereDoesntHaveRelation Methods image

Laravel introduces new methods to streamline negative relation queries, making it easier to find records that lack specific relationships while maintaining clean, readable code.

The new whereDoesntHaveRelation methods provide a concise way to query records without specific relations:

User::whereDoesntHaveRelation(
    'posts', 
    'published_at', 
    '>', 
    now()->subWeek()
)->get();

User::whereMorphDoesntHaveRelation(
    'activities',
    [Comment::class, Review::class],
    'is_featured',
    true
)->get();

These methods prove particularly valuable in content management systems:

class ContentManager
{
    public function findDormantAuthors()
    {
        return User::whereDoesntHaveRelation(
            'articles',
            'published_at',
            '>',
            now()->subDays(60)
        )->get();
    }

    public function getUnmoderatedContent()
    {
        return Article::whereDoesntHaveRelation(
            'moderations',
            'reviewed_at',
            '!=',
            null
        )->get();
    }

    public function getUnpopularContent()
    {
        return Article::whereMorphDoesntHaveRelation(
            'reactions',
            [Like::class, Share::class, Bookmark::class],
            'created_at',
            '>',
            now()->subMonth()
        )->get();
    }

    public function archiveStaleContent()
    {
        return Article::query()
            ->whereDoesntHaveRelation('comments', 'id', '!=', null)
            ->whereDoesntHaveRelation('views', 'id', '!=', null)
            ->whereDoesntHaveRelation(
                'updates',
                'created_at',
                '>',
                now()->subMonths(6)
            )
            ->update(['status' => 'archived']);
    }
}

These new methods eliminate the need for complex whereDoesntHave closures, making negative relation queries more intuitive and maintainable while improving code readability.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article