Skip to content

Commit

Permalink
Merge pull request #321 from jhoff/soft_delete_fix
Browse files Browse the repository at this point in the history
[6.0] Don't add soft delete query filter on hard delete models
  • Loading branch information
taylorotwell authored Nov 19, 2018
2 parents 36df615 + d8a8bd7 commit 47e6135
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function shouldBeSearchable()
public static function search($query = '', $callback = null)
{
return new Builder(
new static, $query, $callback, config('scout.soft_delete', false)
new static, $query, $callback, static::usesSoftDelete() && config('scout.soft_delete', false)
);
}

Expand All @@ -117,11 +117,10 @@ public static function makeAllSearchable()
{
$self = new static();

$softDeletes = in_array(SoftDeletes::class, class_uses_recursive(get_called_class())) &&
config('scout.soft_delete', false);
$softDelete = static::usesSoftDelete() && config('scout.soft_delete', false);

$self->newQuery()
->when($softDeletes, function ($query) {
->when($softDelete, function ($query) {
$query->withTrashed();
})
->orderBy($self->getKeyName())
Expand Down Expand Up @@ -169,8 +168,8 @@ public function unsearchable()
*/
public function getScoutModelsByIds(Builder $builder, array $ids)
{
$query = in_array(SoftDeletes::class, class_uses_recursive($this))
? $this->withTrashed() : $this->newQuery();
$query = static::usesSoftDelete()
? $this->withTrashed() : $this->newQuery();

if ($builder->queryCallback) {
call_user_func($builder->queryCallback, $query);
Expand Down Expand Up @@ -321,4 +320,14 @@ public function getScoutKeyName()
{
return $this->getQualifiedKeyName();
}

/**
* Determine if the current class should use soft deletes with searching.
*
* @return bool
*/
protected static function usesSoftDelete()
{
return in_array(SoftDeletes::class, class_uses_recursive(get_called_class()));
}
}
7 changes: 7 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function test_macroable()
);
}

public function test_hard_delete_doesnt_set_wheres()
{
$builder = new Builder($model = Mockery::mock(), 'zonda', null, false);

$this->assertArrayNotHasKey('__soft_deleted', $builder->wheres);
}

public function test_soft_delete_sets_wheres()
{
$builder = new Builder($model = Mockery::mock(), 'zonda', null, true);
Expand Down

0 comments on commit 47e6135

Please sign in to comment.