F
Filament•6d ago
Benjamin

Table filters : column not found, why ?

Hi everyone ! Is someone here capable to explain how/why table filters have no access to table query column ? For example : Let's say I have a users table with a calculated "availabilityScore" column (percentage, from 0 to 100). If I want a filter for that "availabilityScore", I can't just use ->where('availabilityScore', '>', 10) because it will throw a column not found error. Why filters are not just modifying the table query ? I'm sure there is good reasons, I just need to understand 🙂 Thanks !
3 Replies
LeandroFerreira
LeandroFerreira•6d ago
is availabilityScore an accessor?
Benjamin
BenjaminOP•6d ago
No, I make a joinSub() and an addSelect() in the $table->query() method. Accessor could work, I thought about it but was concerned about the performance. With an accessor, it will make one additional query to the availabilities table no ? And I don't need to load the availabilityScore each time I get a user, I only need it for this table so I don't want those data to be load each time I load the user model. For example here, why the $query is a neutral Builder (same as doing User::query()) and not the same query of the table that is used to fill the table, in $table->query() ? Does it mean that, for each filter, it makes another SQL query and then filter the table by IDs comparison ?
Tables\Filters\Filter::make('hasMinimumAvailabilityScore')
->label('Au moins 10% de disponibilité')
->query(fn (Builder $query): Builder => $query->where('availabilityScore', '10')),
Tables\Filters\Filter::make('hasMinimumAvailabilityScore')
->label('Au moins 10% de disponibilité')
->query(fn (Builder $query): Builder => $query->where('availabilityScore', '10')),
LeandroFerreira
LeandroFerreira•6d ago
Filters will use the table columns to filter results. You might consider creating virtual columns for calculated data
Want results from more Discord servers?
Add your server