Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] fix using onlyTrashed and withTrashed with whereHas #13390

Closed
wants to merge 1 commit into from
Closed

[5.2] fix using onlyTrashed and withTrashed with whereHas #13390

wants to merge 1 commit into from

Conversation

themsaid
Copy link
Member

For Post having many Comment models, Comment is using soft deletion:

Post::has('comments')->get(); 
// Returns a post that has comments where `comments.deleted_at IS NULL`.

Post::whereHas('comments', function($q){
    $q->withTrashed();
})->get();
// Should return posts that has comments even if soft deleted.

Post::whereHas('comments', function($q){
    $q->onlyTrashed();
})->get();
// Should return posts that has all comments soft deleted.

Currently case 2 & 3 aren't working, that's because the soft deletion query scope adds a whereNull condition on every query, this PR manages to remove this condition when needed. Here's a sample query that demonstrates case 2:

select * from `posts` where exists (select * from `comments` where `comments`.`post_id` = `posts`.`id` and `comments`.`deleted_at` is not null and `comments`.`deleted_at` is null)

NOTE I'm not sure if that's the right way to accomplish the task, I tried to fix it with a minimal effect on the current code.

$relationWheres = array_filter($relationWheres, function ($where) use ($deletedAtColumn) {
return $where['column'] != $deletedAtColumn;
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this is taking us back to really brittle column name comparisons like we used to have on scopes.

@taylorotwell
Copy link
Member

I'm curious if @acasar has a different solution or alternative to this because I don't want to go back to comparing column names.

@acasar
Copy link
Contributor

acasar commented May 1, 2016

@taylorotwell I think I found a better way to solve this problem: #13396

@themsaid I used your tests, but implemented the logic a bit differently. Can you check if this fixes your issue?

@themsaid
Copy link
Member Author

themsaid commented May 1, 2016

@acasar I think that would do yes, & it's also implemented in a cleaner way 👍
I'm closing this PR then.

@themsaid themsaid closed this May 1, 2016
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.

3 participants