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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,11 +947,48 @@ protected function mergeModelDefinedRelationWheresToHasQuery(Builder $hasQuery,
// the has query, and then copy the bindings from the "has" query to the main.
$relationQuery = $relation->toBase();

$relationWheres = $relationQuery->wheres;

if (in_array(SoftDeletes::class, class_uses($hasQuery->model))) {
$relationWheres = $this->removeSoftDeletionWhereConditionIfNotNeeded($relationWheres, $hasQuery);
}

$hasQuery->withoutGlobalScopes()->mergeWheres(
$relationQuery->wheres, $relationQuery->getBindings()
$relationWheres, $relationQuery->getBindings()
);
}

/**
* Remove soft deletion "whereNull" condition if not needed.
*
* @param array $relationWheres
* @param \Illuminate\Database\Eloquent\Builder $hasQuery
* @return array
*/
protected function removeSoftDeletionWhereConditionIfNotNeeded($relationWheres, Builder $hasQuery)
{
$deletedAtColumn = $hasQuery->model->getQualifiedDeletedAtColumn();

$deletedAtColumnWheres = array_filter($hasQuery->toBase()->wheres, function ($where) use ($deletedAtColumn) {
return isset($where['column']) && $where['column'] == $deletedAtColumn;
});

$deletedAtColumnWhereNotNull = array_filter($deletedAtColumnWheres, function ($where) use ($deletedAtColumn) {
return $where['type'] == 'NotNull';
});

// If no where conditions on the deleted_at column then we need to return all the
// records, so we remove the default condition added by the SoftDeletingScope.
// We also remove the condition if another was added checking for NOT NULL.
if (! $deletedAtColumnWheres || $deletedAtColumnWhereNotNull) {
$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.


return $relationWheres;
}

/**
* Get the "has relation" base query instance.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,31 @@ public function testWhereHasWithDeletedRelationship()
$this->assertEquals(0, count($users));
}

/**
* @group test
*/
public function testWhereHasWithNestedDeletedRelationshipAndOnlyTrashedCondition()
{
$this->createUsers();

$abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
$post = $abigail->posts()->create(['title' => 'First Title']);
$post->delete();

$users = SoftDeletesTestUser::has('posts')->get();
$this->assertEquals(0, count($users));

$users = SoftDeletesTestUser::wherehas('posts', function ($q) {
$q->onlyTrashed();
})->get();
$this->assertEquals(1, count($users));

$users = SoftDeletesTestUser::wherehas('posts', function ($q) {
$q->withTrashed();
})->get();
$this->assertEquals(1, count($users));
}

/**
* @group test
*/
Expand Down