Skip to content

Commit 81ec6c6

Browse files
committed
fixing searchCallback parameter, adding test
1 parent 4dd6be3 commit 81ec6c6

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/Traits/WithFilters.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function applySearchFilter(Builder $query): Builder
230230
if ($column->searchCallback) {
231231

232232
// call the callback
233-
($column->searchCallback)($query, $search);
233+
($column->searchCallback)($subQuery, $search);
234234

235235
// if the column isn't a relation or if it was previously selected
236236
} elseif (! $hasRelation || $selectedColumn) {
@@ -256,6 +256,7 @@ public function applySearchFilter(Builder $query): Builder
256256
}
257257
}
258258
});
259+
259260
}
260261

261262
return $query;

tests/DataTableComponentTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public function test_search_filter_remove(): void
9494
$this->assertEquals(5, $this->table->rows->total());
9595
}
9696

97+
/** @test */
98+
public function test_search_filter_callback(): void
99+
{
100+
$this->table->filters['search'] = '2';
101+
$this->assertEquals(1, $this->table->getRowsProperty()->total());
102+
}
103+
97104
public function test_search_filter_alt_query()
98105
{
99106
$this->tableAltQuery->filters['search'] = 'Cartman';

tests/Http/Livewire/PetsTable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public function columns(): array
2525
Column::make('Name', 'name')
2626
->searchable(),
2727
Column::make('Age', 'age')
28-
->searchable(),
28+
->searchable(function(Builder $query, $search){
29+
$query->orWhere('age', '=', $search);
30+
}),
2931
Column::make('Last Visit', 'last_visit')
3032
->searchable(),
3133
Column::make('Species', 'species.name')

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function setUp(): void
4040
Pet::insert([
4141
[ 'id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4 ],
4242
[ 'id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4 ],
43-
[ 'id' => 3, 'name' => 'May', 'age' => 3, 'species_id' => 2, 'breed_id' => 102 ],
43+
[ 'id' => 3, 'name' => 'May', 'age' => 2, 'species_id' => 2, 'breed_id' => 102 ],
4444
[ 'id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200 ],
4545
[ 'id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202 ],
4646
]);

0 commit comments

Comments
 (0)