Skip to content

Commit 0d80323

Browse files
authored
Merge pull request #1010 from bambamboole/feat/allow-filters-as-array
Allow nested filters
2 parents 7e89e7d + 8ab103e commit 0d80323

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Concerns/FiltersQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function allowedFilters($filters): static
1414
{
1515
$filters = is_array($filters) ? $filters : func_get_args();
1616

17-
$this->allowedFilters = collect($filters)->map(function ($filter) {
17+
$this->allowedFilters = collect($filters)->flatten(1)->map(function ($filter) {
1818
if ($filter instanceof AllowedFilter) {
1919
return $filter;
2020
}

tests/FilterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,20 @@ public function __invoke(Builder $query, $value, string $property): Builder
479479
expect($results->pluck('id')->all())->toEqual([$model1->id, $model2->id]);
480480
});
481481

482+
it('can allow multiple filters as nested array', function () {
483+
$model1 = TestModel::create(['name' => 'abcdef']);
484+
$model2 = TestModel::create(['name' => 'abcdef']);
485+
486+
$results = createQueryFromFilterRequest([
487+
'name' => 'abc',
488+
])
489+
->allowedFilters([['name'], [AllowedFilter::exact('id')]])
490+
->get();
491+
492+
expect($results)->toHaveCount(2);
493+
expect($results->pluck('id')->all())->toEqual([$model1->id, $model2->id]);
494+
});
495+
482496
it('can filter by multiple filters', function () {
483497
$model1 = TestModel::create(['name' => 'abcdef']);
484498
$model2 = TestModel::create(['name' => 'abcdef']);

0 commit comments

Comments
 (0)