SelectFilter with options from custom query

I am trying to show a SelectFilter for possible values of a specific column. I tried using the options() method and passing an Eloquent query in a local copy of the Filament demo application, like so:
SelectFilter::make('name')->options(Product::all()->pluck('name', 'name'))
SelectFilter::make('name')->options(Product::all()->pluck('name', 'name'))
` Got the idea of this approach from https://filamentphp.com/tricks/filter-by-multiple-spatie-tags. It is working a expected but in Debugbar i see a bunch of database requests. Normally executing one should be enough to fill the SelectFilter. Looks like every Trait (HasActions, HasBulkActions,...) is triggering it. Am I missing something? Doing So many requests is not really a solution at the moment.
3 Replies
toeknee
toeknee2y ago
Firstly it needs to be a closure so change:
SelectFilter::make('name')->options(Product::all()->pluck('name', 'name'))
SelectFilter::make('name')->options(Product::all()->pluck('name', 'name'))
to
SelectFilter::make('name')->options(fn() => Product::all()->pluck('name', 'name'))
SelectFilter::make('name')->options(fn() => Product::all()->pluck('name', 'name'))
daerentis
daerentisOP2y ago
Ohhhh, right! That solved it and saved me some hours, thank you very much!
toeknee
toeknee2y ago
Welcomes!
Want results from more Discord servers?
Add your server