How to make table searchable?

I'm in the admin panel, trying to apply a custom search to the table, but im unable to show the search input :/ this is what im trying:
<?php

namespace App\Filament\Resources\ResponseResource\Pages;

use App\Enums\FormFieldTypeEnum;
use App\Filament\Resources\ResponseResource;
use App\Filament\Resources\ResponseResource\Widgets\RadioChart;
use App\Filament\Resources\ResponseResource\Widgets\RangeChart;
use App\Filament\Traits\HasParentResource;
use App\Models\Form;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Support\Enums\MaxWidth;
use Illuminate\Database\Eloquent\Builder;

class ListResponses extends ListRecords
{
use HasParentResource;

protected static string $resource = ResponseResource::class;

// protected function search
public function isTableSearchable(): bool
{
return true;
}

// public function hasTableSearch(): bool
// {
// return true;
// }

protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($search = $this->getTableSearch())) {
// dd($search);
// $query->saerch
}
return $query;
}
}
<?php

namespace App\Filament\Resources\ResponseResource\Pages;

use App\Enums\FormFieldTypeEnum;
use App\Filament\Resources\ResponseResource;
use App\Filament\Resources\ResponseResource\Widgets\RadioChart;
use App\Filament\Resources\ResponseResource\Widgets\RangeChart;
use App\Filament\Traits\HasParentResource;
use App\Models\Form;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Support\Enums\MaxWidth;
use Illuminate\Database\Eloquent\Builder;

class ListResponses extends ListRecords
{
use HasParentResource;

protected static string $resource = ResponseResource::class;

// protected function search
public function isTableSearchable(): bool
{
return true;
}

// public function hasTableSearch(): bool
// {
// return true;
// }

protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($search = $this->getTableSearch())) {
// dd($search);
// $query->saerch
}
return $query;
}
}
14 Replies
ericmp #2
ericmp #2OP13mo ago
Filament
Customize table search by Z3d0X - Tricks - Filament
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
ericmp #2
ericmp #2OP13mo ago
i know is v2, and im using v3, but should be similar, idk how
ConnorHowell
ConnorHowell13mo ago
Have you made any columns searchable?
ericmp #2
ericmp #2OP13mo ago
no is mandatory? i made a column searchable, now it appears. but there is no way to make the search appear without making a col searchable?
ConnorHowell
ConnorHowell13mo ago
Nope, you should just be able to do ->searchable() on the table itself? Or instead of isTableSearchable you can override isSearchable
ericmp #2
ericmp #2OP13mo ago
the thing is that i wanna search a json column containing an array of items :/
ConnorHowell
ConnorHowell13mo ago
Guessing it's a custom form type system where you have a json column with the response values?
ericmp #2
ericmp #2OP13mo ago
yes this is what im trying now:
protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($search = $this->getTableSearch())) {
// $query->whereJsonContains('responses', $search);

// $query->whereJsonContains('responses', ['nom' => $search]);
$query->where('responses', 'like', "%{$search}%");
}

return $query;
}
protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($search = $this->getTableSearch())) {
// $query->whereJsonContains('responses', $search);

// $query->whereJsonContains('responses', ['nom' => $search]);
$query->where('responses', 'like', "%{$search}%");
}

return $query;
}
if the column is a json object, works - i have it done in another project but if the column is an array of objects, it doesnt hmmmm
ConnorHowell
ConnorHowell13mo ago
Ahh I see, on mine I generated the columns based on the forms schema then implemented a custom searchable query for each. So have you managed to get the search box to pop up now just the query isn't working?
ericmp #2
ericmp #2OP13mo ago
yeah sorry, i managed it to pop up, this should be a diferent question 🙌 and this seems that does the trick - $query->where('responses', 'like', "%{$search}%"); i guess as json col is a string, it just searches though it i thought didnt work but i guess had to reload or something, now works oh wait a second wtf
ConnorHowell
ConnorHowell13mo ago
Yeah, that should be fine unless you're wanting to search a specific column, think you can treat it like an object in whereJsonContains if you wanted to search specific columns
ericmp #2
ericmp #2OP13mo ago
now doesnt work let me clear cahce or something ah, is the case i was searching "et et", and one of my rows has "Et et", so nothing was appearing if i search it like this: "Et et", i see it hmm okay now im going to search how to make case insensitive
ConnorHowell
ConnorHowell13mo ago
Just strtoupper/lower both the column contents and your search term
ericmp #2
ericmp #2OP13mo ago
yeah cool final method:
protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($search = $this->getTableSearch())) {
$search = str($search)->trim()->lower()->toString();

$query->whereRaw('LOWER(responses) LIKE ?', ['%' . $search . '%']);
}

return $query;
}
protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($search = $this->getTableSearch())) {
$search = str($search)->trim()->lower()->toString();

$query->whereRaw('LOWER(responses) LIKE ?', ['%' . $search . '%']);
}

return $query;
}
thanks for the help (:

Did you find this page helpful?