Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 29, 2021
1 parent f04927d commit b95af2e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/Engines/CollectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,9 @@ protected function searchModels(Builder $builder)
})
->orderBy($builder->model->getKeyName(), 'desc');

if (Arr::get($builder->wheres, '__soft_deleted') === 0) {
$query = $query->withoutTrashed();
} elseif (Arr::get($builder->wheres, '__soft_deleted') === 1) {
$query = $query->onlyTrashed();
} elseif (in_array(SoftDeletes::class, class_uses_recursive(get_class($builder->model))) &&
config('scout.soft_delete', false)) {
$query = $query->withTrashed();
}

$models = $query->get()->values();
$models = $this->ensureSoftDeletesAreHandled($builder, $query)
->get()
->values();

if (count($models) === 0) {
return $models;
Expand All @@ -130,6 +123,27 @@ protected function searchModels(Builder $builder)
})->values();
}

/**
* Ensure that soft delete handling is properly applied to the query.
*
* @param \Laravel\Scout\Builder $builder
* @param \Illuminate\Database\Query\Builder $query
* @return \Illuminate\Database\Query\Builder
*/
protected function ensureSoftDeletesAreHandled($builder, $query)
{
if (Arr::get($builder->wheres, '__soft_deleted') === 0) {
return $query->withoutTrashed();
} elseif (Arr::get($builder->wheres, '__soft_deleted') === 1) {
return $query->onlyTrashed();
} elseif (in_array(SoftDeletes::class, class_uses_recursive(get_class($builder->model))) &&
config('scout.soft_delete', false)) {
return $query->withTrashed();
}

return $query;
}

/**
* Pluck and return the primary keys of the given results.
*
Expand Down

0 comments on commit b95af2e

Please sign in to comment.