Pass function inside table filters().

Is it possible to pass a function that returns array inside the ->filters() since my filters are dynamic.
16 Replies
Patrick Boivin
No... but maybe you can implement dynamic filters by overriding getTableFilters() on your page class instead. Never tried but I'm curious.
Patrick Boivin
This is a v2 question
AlexAnder
AlexAnder2y ago
V2 docs the same
Patrick Boivin
I understand... What do you think can be useful on this page to solve the issue?
AlexAnder
AlexAnder2y ago
You may customize the database query that retrieves options using the third parameter of the relationship() method..
lodeki
lodekiOP2y ago
@alexander7479 @pboivin Sorry to say but i don't really understand .Could you elaborate?
AlexAnder
AlexAnder2y ago
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Database\Eloquent\Builder;

SelectFilter::make('author')
->relationship('author', 'name', fn (Builder $query) => $query->withTrashed())
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Database\Eloquent\Builder;

SelectFilter::make('author')
->relationship('author', 'name', fn (Builder $query) => $query->withTrashed())
Create model relationship, then use this relationship in filter and also customize the query..
lodeki
lodekiOP2y ago
Okay, let me work it out.Thank you.
Patrick Boivin
Can you describe how your filters are dynamic? What changes according to what...
lodeki
lodekiOP2y ago
I have a utilities table ( eg gym,internet,garbage etc ) This utilities can be CRUDed .I want to have filters for each of this so that if the active utility is say garbage, the tenants table should now filter tenants who have this utility.
Patrick Boivin
So at first, the 'tenant' filter is not visible and if I select a value in the 'utility' filter, then the 'tenant' filter appears? Or is it the values that change? All filters are visible but when I select 'internet', the values in the 'tenant' select are limited to the tenants that have internet?
lodeki
lodekiOP2y ago
Yes sure the second option * All filters are visible but when I select 'internet', the values in the 'tenant' select are limited to the tenants that have internet? * @pboivin Sorry for responding late.Had internet challenges.
Patrick Boivin
@lodeki No worries. I'm not sure you'll be able to do this with relationship()... I have a feeling that a custom filter form will give you more flexibility : https://beta.filamentphp.com/docs/2.x/tables/filters#custom-filter-forms
lodeki
lodekiOP2y ago
I used the SelectFilter . It did the trick .Thank you.

Did you find this page helpful?