Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/Filters/FiltersOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function __invoke(Builder $query, $value, string $property)
} elseif ($this->filterOperator->isDynamic() && $value !== null) {
$filterOperator = $this->getDynamicFilterOperator($value);
$this->removeDynamicFilterOperatorFromValue($value, $filterOperator);
} elseif ($this->filterOperator->isDynamic() && $value === null) {
$filterOperator = FilterOperator::EQUAL;
}

if ($value === '') {
$value = null;
}

$query->where($query->qualifyColumn($property), $filterOperator->value, $value, $this->boolean);
Expand Down
29 changes: 29 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,35 @@ public function __invoke(Builder $query, $value, string $property): Builder
expect($results->first()->name)->toBeNull();
});

it('can filter with dynamic operator filter when value is empty string', function () {
TestModel::create(['name' => null, 'salary' => 1000]);
TestModel::create(['name' => 'John', 'salary' => 2000]);

$results = createQueryFromFilterRequest([
'name' => '',
])
->allowedFilters(AllowedFilter::operator('name', FilterOperator::DYNAMIC)->nullable())
->get();

expect($results)->toHaveCount(1);
expect($results->first()->name)->toBeNull();
});

it('can filter with not-equal dynamic operator filter when value is empty after operator', function () {
TestModel::create(['name' => null, 'salary' => 1000]);
TestModel::create(['name' => 'John', 'salary' => 2000]);

$results = createQueryFromFilterRequest([
'name' => '<>',
])
->allowedFilters(AllowedFilter::operator('name', FilterOperator::DYNAMIC)->nullable())
->get();

// Empty value after <> produces IS NOT NULL: null-name model excluded, named model included
expect($results->pluck('name')->contains(null))->toBeFalse();
expect($results->where('salary', 2000)->first()?->name)->toBe('John');
});

it('throws RelationNotFoundException when the relation method exists but does not return an Eloquent Relation', function () {
$ModelWithNonRelationMethod = new class () extends TestModel {
protected $table = 'test_models';
Expand Down