Autopopulate SelectFilter by table values?

Hi all! Is it possible to add a SelectFilter without defining the options manually when you want a filter based on the unique values in the table? Let's say you have a PostsTable where the category of the post is a string value in the posts table. Can you add a SelectFilter for this category column without defining the options and the the Table/Filter will find the unique options by itself?
20 Replies
toeknee
toeknee2mo ago
Yes you can use a query in the options?
->options(fn () => PostsTable::select('category')->whereNotNull('category')->distinct()->pluck('category', 'category')),
->options(fn () => PostsTable::select('category')->whereNotNull('category')->distinct()->pluck('category', 'category')),
Mike Peters
Mike PetersOP2mo ago
@toeknee yes but then I'm still running the query, wondering if the filter could auto populate with the values it finds in the DB (it already queries the db to get the values) So if I name it x it automatically fetches the values for x by itself. But given your answer I can imagine this doesn't exist yet. thanks though!
toeknee
toeknee2mo ago
Do you mean you want a dependable filter? That changes based of another filters value? I’m struggling to fill understand what you are looking for
Mike Peters
Mike PetersOP2mo ago
So imagine I have table with posts and I'd like to just add SelectFilter::make('category') and then Filament could just by itself find the values for category that are in the db right now and magically make the filter. Was wondering if that existed.
toeknee
toeknee2mo ago
It does that? ->relationship(‘category’) And providing your post has a model it will build it for you, and will use name as the label. You can add ,’title’ to to the relationship to change from name to title for the label
Mike Peters
Mike PetersOP2mo ago
yes, but then for normal values in the DB, so let's say the posts have a type which is just a string, autopopulate from there good that you mention, so the relationship variant works exactly like what I mean, but then I mean for regular values without relationships
toeknee
toeknee2mo ago
Then just do a DB Fetch? for them with distinct()?
Florin
Florin2mo ago
Yes but is not a relationship, basicly i want to be able to select the second filter dependent on the first one, I have the fallowing "SelectFilter::make('make_id') ->label('Marca') ->options(fn() => Stock::with('make') ->selectRaw('make_id, COUNT() as count') ->groupBy('make_id') ->get() ->mapWithKeys(fn($item) => [$item->make_id => optional($item->make)->name." ({$item->count})"]) ->toArray() ) ->multiple() ->searchable(), SelectFilter::make('model') ->label('Model') ->options(fn() => Stock::selectRaw('model, COUNT() as count') ->groupBy('model') ->get() ->mapWithKeys(fn($item) => [$item->model => "{$item->model} ({$item->count})"]) ->toArray() ) ->multiple() ->searchable()," and I want the model to be from the make_id, this is on tables I ve tried something like this "Filter::make('vehicle_filter') ->form([ Select::make('make_id') ->label('Marca') ->options(fn() => Stock::with('make') ->selectRaw('make_id, COUNT() as count') ->groupBy('make_id') ->get() ->mapWithKeys(fn($item) => [ $item->make_id => optional($item->make)->name . " ({$item->count})" ]) ->toArray() ) ->multiple() ->searchable(), Select::make('model') ->label('Model') ->options(fn(callable $get) => Stock::when(filled($get('make_id')), fn($query) => $query->whereIn('make_id', $get('make_id')) ) ->selectRaw('model, COUNT() as count') ->groupBy('model') ->get() ->mapWithKeys(fn($item) => [$item->model => "{$item->model} ({$item->count})"]) ->toArray() ) ->multiple() ->searchable(), ]) ->query(function (Builder $query, array $data): Builder { return $query ->when(!empty($data['make_id']), fn($query) => $query->whereIn('make_id', $data['make_id'])) ->when(!empty($data['model']), fn($query) => $query->whereIn('model', $data['model'])); });" but the query is not being executed
toeknee
toeknee2mo ago
So it's not live. Something like this is what I suspect you want.:
SelectFilter::make('make_id')
->label('Marca')
->options(fn() => Stock::with('make')
->selectRaw('make_id, COUNT() as count')
->groupBy('make_id')
->get()
->mapWithKeys(fn($item) => [$item->make_id => optional($item->make)->name." ({$item->count})"])
->toArray()
)
->multiple()
->searchable(),
SelectFilter::make('model')
->label('Model')
->disabled(fn($get) => empty($get('make_id')))
->options(fn($get) => Stock::selectRaw('model, COUNT() as count')
->where('make_id', (int) $get('make_id'))
->groupBy('model')
->get()
->mapWithKeys(fn($item) => [$item->model => "{$item->model} ({$item->count})"])
->toArray()
)
->multiple()
->searchable(),
SelectFilter::make('make_id')
->label('Marca')
->options(fn() => Stock::with('make')
->selectRaw('make_id, COUNT() as count')
->groupBy('make_id')
->get()
->mapWithKeys(fn($item) => [$item->make_id => optional($item->make)->name." ({$item->count})"])
->toArray()
)
->multiple()
->searchable(),
SelectFilter::make('model')
->label('Model')
->disabled(fn($get) => empty($get('make_id')))
->options(fn($get) => Stock::selectRaw('model, COUNT() as count')
->where('make_id', (int) $get('make_id'))
->groupBy('model')
->get()
->mapWithKeys(fn($item) => [$item->model => "{$item->model} ({$item->count})"])
->toArray()
)
->multiple()
->searchable(),
Florin
Florin2mo ago
Yes, but there is no live method or disabled on table SelectFIlter also An attempt was made to evaluate a closure for [Filament\Tables\Filters\SelectFilter], but [$get] was unresolvable.
toeknee
toeknee2mo ago
Ahh of course, you need to use a form method instead
toeknee
toeknee2mo ago
Similar to how hugh recommends it here https://v2.filamentphp.com/tricks/dependent-select-filters
Filament
Dependent Select Filters by Hugh Messenger - Tricks - Filament
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
toeknee
toeknee2mo ago
which is similar to what you did
Florin
Florin2mo ago
I ve tried it like this, but there is no query executed when form is selected
toeknee
toeknee2mo ago
It should, are you overriding the tables query? in $table
Florin
Florin2mo ago
how to do that?
toeknee
toeknee2mo ago
I don't want you too. I am asking if you are. Can you provide the whole resource?
Florin
Florin2mo ago
Florin
Florin2mo ago
is possible to move the table filters on the left side?
toeknee
toeknee2mo ago
Not out of the box, you could try some custom CSS but you'd need to flip it. You could also put the filters inline / always displayed.

Did you find this page helpful?