FilamentF
Filament14mo ago
Neamix

Dynamic Filter

I want to create dynamic filter like step filter example i have table pet and each pet have type and each type contain a group of species so what i want to do here to allow user to choose the type first then the species appear to him i tried that concept
->filters([
                SelectFilter::make('pet_type_id')
                    ->label('نوع الحيوان')
                    ->query(fn($data) => Session::put('pet.pet_type_id',$data['value'] ?? null))
                    ->options(PetType::pluck('name_ar','id')->toArray()),

                SelectFilter::make('pet_specie_id')
                    ->options(function() {
                        $selectedPetType = Session::get('pet.pet_type_id');
                        dump($selectedPetType);
                        if ($selectedPetType)
                            return PetSpecie::where('pet_type_id',$selectedPetType)->pluck('specie_ar','id')->toArray();
                        else 
                            return PetSpecie::pluck('specie_ar','id')->toArray();
                    })
                    ->label('فصيلة الحيوان'),

                SelectFilter::make('pet_color_id')
                    ->label('لون الحيوان')
                    ->options(PetColor::pluck('color_ar', 'id')->toArray()),
            ])


the only reason that i used query() because it was the only thing that worked as a callback function and this approche worked but the problem is that he get the prev type as example if i have type 0 default ,1 ,2 if i changed type from 0 to 1 it will show species of 0 if i changed 1 to 2 it will show species of 1 and so one
Was this page helpful?