Skip to content

Commit 72f8de3

Browse files
authored
Merge pull request spatie#533 from dominikb/Issue-495_tighten_ignored_filter_value_definition
Update what defines an ignored filter value
2 parents 178628f + 711306c commit 72f8de3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Filters/FiltersPartial.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public function __invoke(Builder $query, $value, string $property)
2121
$sql = "LOWER({$wrappedProperty}) LIKE ?";
2222

2323
if (is_array($value)) {
24-
if (count(array_filter($value)) === 0) {
24+
if (count(array_filter($value, 'strlen')) === 0) {
2525
return $query;
2626
}
2727

2828
$query->where(function (Builder $query) use ($value, $sql) {
29-
foreach (array_filter($value) as $partialValue) {
29+
foreach (array_filter($value, 'strlen') as $partialValue) {
3030
$partialValue = mb_strtolower($partialValue, 'UTF8');
3131

3232
$query->orWhereRaw($sql, ["%{$partialValue}%"]);

tests/FilterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ public function it_ignores_an_empty_array_partial_filter()
154154
$this->assertCount(5, $results);
155155
}
156156

157+
/** @test */
158+
public function falsy_values_are_not_ignored_when_applying_a_partial_filter()
159+
{
160+
DB::enableQueryLog();
161+
162+
$this
163+
->createQueryFromFilterRequest([
164+
'id' => [0],
165+
])
166+
->allowedFilters(AllowedFilter::partial('id'))
167+
->get();
168+
169+
$this->assertQueryLogContains("select * from `test_models` where (LOWER(`id`) LIKE ?)");
170+
}
171+
157172
/** @test */
158173
public function it_can_filter_and_match_results_by_exact_property()
159174
{

0 commit comments

Comments
 (0)