Skip to content

Commit c5148ec

Browse files
committed
Merge branch 'feature-searchable' of https://github.com/DeltaSystems/laravel-livewire-tables into DeltaSystems-feature-searchable
# Conflicts: # src/Traits/WithFilters.php # tests/DataTableComponentTest.php
2 parents 2ecdbc0 + 7f5f00d commit c5148ec

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,16 @@ The search is a special built-in filter that is managed by the component, but yo
351351

352352
```php
353353
Column::make('Type')
354-
->searchable()
354+
->searchable(),
355+
```
356+
357+
You can also pass a callback for more control:
358+
359+
```php
360+
Column::make('Type')
361+
->searchable(function (Builder $query, $searchTerm) {
362+
$query->orWhere(...);
363+
}),
355364
```
356365

357366
Sometimes the default search behavior may not meet your requirements. If this is the case, skip using the searchable() method on columns and define your own behavior directly on the query.

src/Traits/WithFilters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function applySearchFilter(Builder $query): Builder
223223
// If the column has a search callback, just use that
224224
if ($column->hasSearchCallback()) {
225225
// Call the callback
226-
($column->getSearchCallback())($query, $search);
226+
($column->getSearchCallback())($subQuery, $search);
227227
} elseif (! $hasRelation || $selectedColumn) { // If the column isn't a relation or if it was previously selected
228228
$whereColumn = $selectedColumn ?? $column->column();
229229

tests/DataTableComponentTest.php

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

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

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)