F
Filamentβ€’6mo ago
Jamie Cee

Conditional Filtering

Im assuming this is possible with custom functionality, but just wanted to check prior if there is any easy/simple solutions. I have a select filter based on a location state, but when I select a state, I want to then show another select with all cities under that state (and stay hidden if none exist).
9 Replies
Jamie Cee
Jamie Ceeβ€’6mo ago
This is what I have so far
Filter::make('location')
->form([
Select::make('state')
->options(Location::all()->pluck('state', 'state')),
Select::make('city')
->options(
Location::all()->pluck('city', 'city'),
)
->hiddenOn('state', null),
// ->query(function (Builder $query, $state) {
// if ($state['value'] == null) {
// return $query;
// } else {
// return $query->whereHas('location', function ($q) use ($state) {
// return $q->where('state', $state['value']);
// });
// }
// }),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['state'],
fn (Builder $query, $state) => $query->whereHas('location', fn (Builder $query) => $query->where('state', $state))
)
->when(
$data['city'],
fn (Builder $query, $city) => $query->whereHas('location', fn (Builder $query) => $query->where('city', $city))
);
}),
Filter::make('location')
->form([
Select::make('state')
->options(Location::all()->pluck('state', 'state')),
Select::make('city')
->options(
Location::all()->pluck('city', 'city'),
)
->hiddenOn('state', null),
// ->query(function (Builder $query, $state) {
// if ($state['value'] == null) {
// return $query;
// } else {
// return $query->whereHas('location', function ($q) use ($state) {
// return $q->where('state', $state['value']);
// });
// }
// }),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['state'],
fn (Builder $query, $state) => $query->whereHas('location', fn (Builder $query) => $query->where('state', $state))
)
->when(
$data['city'],
fn (Builder $query, $city) => $query->whereHas('location', fn (Builder $query) => $query->where('city', $city))
);
}),
I need the second select to hide when state is null, but also need to make the option in the second select filter depending on the value of the first select.
Jamie Cee
Jamie Ceeβ€’6mo ago
Yeah, think I got the function part working, following this: https://www.youtube.com/watch?v=ssymfBywqOY
Code With Tony
YouTube
7 Filament Dependant Select Options - FilamentPHP V3 Tutorial
In this video, we will learn how to create dependant select options in FilamentPHP V3. We will start by creating a simple form with three select inputs, one for countries, one for states and one for cities. Then, we will use the $get() to make the state select input dependent on the country select input. This means that the options in the state...
Jamie Cee
Jamie Ceeβ€’6mo ago
Wasnt actually as hard as I thought it was going to be
Lara Zeus
Lara Zeusβ€’6mo ago
the power of filament πŸ’ͺ
Jamie Cee
Jamie Ceeβ€’6mo ago
Im falling in love with it day after day like We're beginning to implement it at work
Lara Zeus
Lara Zeusβ€’6mo ago
thats amazing πŸ‘Œ
Jamie Cee
Jamie Ceeβ€’6mo ago
Fair to say, I done a good sales pitch for it 🀣 And had to flex that it includes my first public github contribution πŸ˜‰
Want results from more Discord servers?
Add your server
More Posts