FilamentF
Filament2y ago
Wim

Tabs with dropdown

I have an ENUM as follows:

enum AnimalSize: string implements HasLabel
{
    case small = 'Small';
    case medium = 'Medium';
    case large = 'Large';
    case verylarge = 'Very Large';

    public function getLabel(): ?string
    {
        return match ($this) {
            self::small => 'Small',
            self::medium => 'Medium',
            self::large => 'Large',
            self::verylarge => 'Very Large',
        };
    }
}


I want to use tabs above a table with all animals. Currently I'm doing it as follows (looks as in the screenshot):

$tabs['small'] = Tab::make('Small')
            ->badge(Animal::where('size', 'Small')->count())
            ->modifyQueryUsing(fn (Builder $query) => $query->where('size', 'Small'));

It works but it gets quite lengthy as my animals table has lots of features.

1) Is there a way I could use a dropdown in the tabs called Size with values from the ENUM?
2) Instead of repeating the code for all different sizes, can I use the ENUM class in the modifyQueryUsing?
Solution
Thanks. Implemented it and leaving it here for others:

   public function getTabs(): array
    {
        $animal_sizes = AnimalSize::cases();
        $animal_types = AnimalType::cases();

        $tabs = [
            'all' => Tab::make()
                ->badge((string)Animal::count())
        ];
        foreach ($animal_sizes as $size) {
            $tabs[Str::headline($size->value)] = Tab::make()
                ->badge((string)Animal::query()->where('size', $size)->count())
                ->modifyQueryUsing(fn (Builder $query) => $query->where('size', $size));
        }

        foreach ($animal_types as $type) {
            $tabs[Str::headline($type->value)] = Tab::make()
                ->badge((string)Animal::query()->where('animal_type', $type)->count())
                ->modifyQueryUsing(fn (Builder $query) => $query->where('animal_type', $type));
        }

        return $tabs;
    }
Was this page helpful?