Dynamic getTableQuery on Toggle Filter
Hello everyone. For many cases, I want to dynamically return different query from getTableQuery based on toggle filter. Initially, we want the X query with some records (toggle OFF). But when we toggle ON a filter, execute Y query to return new records. For example:
Toggle filter example:
protected function getTableFilters(): array
{
return [
Tables\Filters\Filter::make('notifikasi_pemeliharaan')
->query(fn (Builder $query): Builder => $query->where('notifikasi_pemeliharaan', false))
->columnSpan('full')
->toggle(),
];
}
Query before (When Toggle filter OFF)
protected function getTableQuery(): Builder
{
return Peralatan::query()
->where('aktif_tidak', true)
->where('pelihara_tidak', true)
});
}
Query expected, after toggle ON
protected function getTableQuery(): Builder
{
return Peralatan::query()
->where('aktif_tidak', true)
->where('pelihara_tidak', true)
->where('notifikasi_pemeliharaan', true) // Now it pull new records that not exists in table before
});
}
How to make it possible? Or is there any better approach? Thank you all.3 Replies
Did you see:
https://filamentphp.com/docs/2.x/tables/filters#custom-filter-forms
convert your arrow function into a function and you can then condition it on the filter data too. So you don't need to do it within the tableQuery
Filament
Filters - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
thank you, i'll take a look
It's working with Select component form using custom filter. I can modify the null value. If I use Toggle component, is there a way to hide queryString filter from showing up in address bar for false value? For example, when I load list page, can I just show http://localhost:8000/peralatan instead of http://localhost:8000/peralatan?tableFilters[komponen_bukan][komponen_bukan]=0
@Umardi im running into the same issue with the always on query string when usign a toggle filter. Did you ever figure this out?