Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #382: add `FilterTrashed` * #382: fix code style * #382: fix naming Co-authored-by: Alex Vanderbist <alex.vanderbist@gmail.com>
- Loading branch information
1 parent
d5af7a9
commit 83c1022
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Spatie\QueryBuilder\Filters; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
/** | ||
* FiltersTrashed provides filter for soft deleted (trashed) records. | ||
* | ||
* This filter responds to particular values: | ||
* | ||
* - 'with' - include 'trashed' records to the result set. | ||
* - 'only' - return only 'trashed' records at the result set. | ||
* - any other - return only records without 'trashed' at the result set. | ||
* | ||
* Usage example: | ||
* | ||
* ```php | ||
* QueryBuilder::for(Item::class) | ||
* ->allowedFilters([ | ||
* AllowedFilter::trashed(), | ||
* // ... | ||
* ]); | ||
* ``` | ||
* | ||
* @see \Illuminate\Database\Eloquent\SoftDeletes | ||
* @see \Spatie\QueryBuilder\AllowedFilter::trashed() | ||
*/ | ||
class FiltersTrashed implements Filter | ||
{ | ||
/** {@inheritdoc} */ | ||
public function __invoke(Builder $query, $value, string $property) | ||
{ | ||
if ($value === 'with') { | ||
$query->withTrashed(); | ||
|
||
return; | ||
} | ||
|
||
if ($value === 'only') { | ||
$query->onlyTrashed(); | ||
|
||
return; | ||
} | ||
|
||
$query->withoutTrashed(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Spatie\QueryBuilder\Tests; | ||
|
||
use Illuminate\Http\Request; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
use Spatie\QueryBuilder\AllowedFilter; | ||
use Spatie\QueryBuilder\Tests\TestClasses\Models\SoftDeleteModel; | ||
|
||
class FiltersTrashedTest extends TestCase | ||
{ | ||
/** @var \Illuminate\Support\Collection */ | ||
protected $models; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->models = factory(SoftDeleteModel::class, 2)->create() | ||
->merge(factory(SoftDeleteModel::class, 1)->create(['deleted_at' => now()])); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_filter_not_trashed_by_default() | ||
{ | ||
$models = $this | ||
->createQueryFromFilterRequest([ | ||
'trashed' => '', | ||
]) | ||
->allowedFilters(AllowedFilter::trashed()) | ||
->get(); | ||
|
||
$this->assertCount(2, $models); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_filter_only_trashed() | ||
{ | ||
$models = $this | ||
->createQueryFromFilterRequest([ | ||
'trashed' => 'only', | ||
]) | ||
->allowedFilters(AllowedFilter::trashed()) | ||
->get(); | ||
|
||
$this->assertCount(1, $models); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_filter_with_trashed() | ||
{ | ||
$models = $this | ||
->createQueryFromFilterRequest([ | ||
'trashed' => 'with', | ||
]) | ||
->allowedFilters(AllowedFilter::trashed()) | ||
->get(); | ||
|
||
$this->assertCount(3, $models); | ||
} | ||
|
||
protected function createQueryFromFilterRequest(array $filters): QueryBuilder | ||
{ | ||
$request = new Request([ | ||
'filter' => $filters, | ||
]); | ||
|
||
return QueryBuilder::for(SoftDeleteModel::class, $request); | ||
} | ||
} |