F
Filament16mo ago
Mikazor

Add custom query to a filter

Tables\Filters\SelectFilter::make('causer_id')
->query(function (Builder $query, array $data): Builder {
return $query->where('id', 1);
})
Tables\Filters\SelectFilter::make('causer_id')
->query(function (Builder $query, array $data): Builder {
return $query->where('id', 1);
})
When I set a filter like in the example above my table always filtered and showed only one record with ID 1. I didn't apply the filter and the query string is empty. I want to add a custom query to a filter. How to achieve this?
Solution:
query exists for the other filters like Filter and TernaryFilter. With SelectFilter, the query is handled internally for you so it acts a little differently.
Jump to solution
5 Replies
Kenneth Sese
Kenneth Sese16mo ago
What are you trying to accomplish? If you just need to filter one option, then use a normal Filter and not a Select Filter. A SelectFilter should be used for multiple options. So if you just want to filter where causer_id == 1, then you could do:
Filter::make('causer_id')
->query(fn (Builder $query): Builder => $query->where('causer_id', 1))
Filter::make('causer_id')
->query(fn (Builder $query): Builder => $query->where('causer_id', 1))
Mikazor
MikazorOP16mo ago
This is just a simple example. I have a more complex logic but I want to show that $query->where('causer_id', 1) always applies to a whole table
Kenneth Sese
Kenneth Sese16mo ago
Ok…so does using Filter::make()… work for you?
Mikazor
MikazorOP16mo ago
Yes, it works. But I'm not sure for what purpose ->query() exists. Am I not using it right?
Solution
Kenneth Sese
Kenneth Sese16mo ago
query exists for the other filters like Filter and TernaryFilter. With SelectFilter, the query is handled internally for you so it acts a little differently.
Want results from more Discord servers?
Add your server