F
Filament17mo ago
lodeki

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
Patrick Boivin17mo ago
No... but maybe you can implement dynamic filters by overriding getTableFilters() on your page class instead. Never tried but I'm curious.
Patrick Boivin
Patrick Boivin17mo ago
This is a v2 question
AlexAnder
AlexAnder17mo ago
V2 docs the same
Patrick Boivin
Patrick Boivin17mo ago
I understand... What do you think can be useful on this page to solve the issue?
AlexAnder
AlexAnder17mo ago
You may customize the database query that retrieves options using the third parameter of the relationship() method..
lodeki
lodekiOP17mo ago
@alexander7479 @pboivin Sorry to say but i don't really understand .Could you elaborate?
AlexAnder
AlexAnder17mo 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
lodekiOP17mo ago
Okay, let me work it out.Thank you.
Patrick Boivin
Patrick Boivin17mo ago
Can you describe how your filters are dynamic? What changes according to what...
lodeki
lodekiOP17mo 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
Patrick Boivin17mo ago
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
lodekiOP17mo 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
Patrick Boivin17mo ago
@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
lodekiOP17mo ago
I used the SelectFilter . It did the trick .Thank you.
Want results from more Discord servers?
Add your server