zouhair lyz
How to dynamically update Province select based on Region select in Filament Tables filter?
->filters([
Filter::make('region_and_province')
->label('Région & Province')
->form([
Select::make('region_id')
->label('Région')
->searchable()
->options(Region::pluck('name', 'id'))
->reactive()
->afterStateUpdated(function ($state, callable $set) {
// Réinitialise la province lorsque la région change
$set('province_id', null);
}),
Select::make('province_id')
->label('Province')
->searchable()
->options(function (callable $get) {
$regionId = $get('region_id'); // Récupérer l'état actuel de la région
if (!$regionId) {
return []; // Si aucune région n'est sélectionnée, retourne une liste vide
}
return Province::where('region_id', $regionId)->pluck('name', 'id')->toArray();
}),
])
->query(function ($query, $data) {
// Applique les filtres Région et Province au query
if (isset($data['region_id']) && $data['region_id']) {
$query->whereHas('province', function ($q) use ($data) {
$q->where('region_id', $data['region_id']);
});
}
if (isset($data['province_id']) && $data['province_id']) {
$query->where('province_id', $data['province_id']);
}
}),
])
15 replies
How to dynamically update Province select based on Region select in Filament Tables filter?
Thank you for the suggestion! I implemented a custom filter combining both Region and Province as described in the documentation, and it works perfectly. Appreciate your help! 😊
15 replies
How to dynamically update Province select based on Region select in Filament Tables filter?
thank you for the suggestion! I’ll explore the option of combining the two filters into one or creating a custom filter as outlined in the documentation. I’ll update here if I find a working solution. 😊
15 replies