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
Yes you can use a query in the options?
@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!
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
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.
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
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
Then just do a DB Fetch? for them with distinct()?
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
So it's not live. Something like this is what I suspect you want.:
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.
Ahh of course, you need to use a form method instead
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.
which is similar to what you did
I ve tried it like this, but there is no query executed when form is selected
It should, are you overriding the tables query?
in $table
how to do that?
I don't want you too. I am asking if you are. Can you provide the whole resource?
is possible to move the table filters on the left side?
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.