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 →
Load Eloquent Models From Form Request Input in Laravel image

Load Eloquent Models From Form Request Input in Laravel

Read article
Laravel FFmpeg 9: Encode Video and Stream HLS and DASH image

Laravel FFmpeg 9: Encode Video and Stream HLS and DASH

Read article
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article