Skip to content

[11.x] Added new Eloquent methods: whereDoesntHaveRelation, whereMorphDoesntHaveRelation and their variants with OR#53996

Merged
taylorotwell merged 1 commit intolaravel:11.xfrom
andrey-helldar:patch/2024-12-21/17-49
Dec 26, 2024
Merged

[11.x] Added new Eloquent methods: whereDoesntHaveRelation, whereMorphDoesntHaveRelation and their variants with OR#53996
taylorotwell merged 1 commit intolaravel:11.xfrom
andrey-helldar:patch/2024-12-21/17-49

Conversation

@andrey-helldar
Copy link
Contributor

@andrey-helldar andrey-helldar commented Dec 21, 2024

Laravel offers to simplify relation queries using the whereRelation and whereMorphRelation methods, but it doesn't have opposite methods. This PR adds them:

// Before
User::whereDoesntHave('comments', function ($query) {
    $query->where('created_at', '>', now()->subDay());
})->get();

// After
User::whereDoesntHaveRelation(
    'comments', 'created_at', '>', now()->subDay()
)->get();

User::whereDoesntHaveRelation(
    'comments', 'is_approved', false
)->get();
// Before
User::whereDoesntHaveMorph('comments', [Post::class, Video::class], function ($query) {
    $query->where('created_at', '>', now()->subDay());
})->get();

// After
User::whereMorphDoesntHaveRelation(
    'comments', [Post::class, Video::class], 'created_at', '>', now()->subDay()
)->get();

User::whereMorphDoesntHaveRelation(
    'comments', [Post::class, Video::class], 'is_approved', false
)->get();

…HaveRelation`, `whereMorphDoesntHaveRelation` and `orWhereMorphDoesntHaveRelation`
@andrey-helldar andrey-helldar changed the title Added new Eloquent methods: whereDoesntHaveRelation, orWhereDoesntHaveRelation, whereMorphDoesntHaveRelation and orWhereMorphDoesntHaveRelation [11.x] Added new Eloquent methods: whereDoesntHaveRelation and whereMorphDoesntHaveRelation Dec 21, 2024
@andrey-helldar andrey-helldar changed the title [11.x] Added new Eloquent methods: whereDoesntHaveRelation and whereMorphDoesntHaveRelation [11.x] Added new Eloquent methods: whereDoesntHaveRelation, whereMorphDoesntHaveRelation and their variants with OR Dec 21, 2024
@taylorotwell taylorotwell merged commit f826d0f into laravel:11.x Dec 26, 2024
@andrey-helldar andrey-helldar deleted the patch/2024-12-21/17-49 branch December 27, 2024 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants