F
Filament12mo 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',
};
}
}
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'));
$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 {...
Jump to solution
2 Replies
DrByte
DrByte12mo ago
You can generate the tabs via a loop. Here's an example of using db records.
public function getTabs(): array
{
$categories = Category::orderBy('sort')->get();

$tabs = [
'all' => Tab::make()
->badge((string)Product::count())
];
foreach ($categories as $category) {
$tabs[Str::headline($category->slug)] = Tab::make()
->modifyQueryUsing(fn (Builder $query) => $query->where('category_id', $category->id))
->badge((string)Product::query()->where('category_id', $category->id)->count());
}

return $tabs;
}
public function getTabs(): array
{
$categories = Category::orderBy('sort')->get();

$tabs = [
'all' => Tab::make()
->badge((string)Product::count())
];
foreach ($categories as $category) {
$tabs[Str::headline($category->slug)] = Tab::make()
->modifyQueryUsing(fn (Builder $query) => $query->where('category_id', $category->id))
->badge((string)Product::query()->where('category_id', $category->id)->count());
}

return $tabs;
}
You could iterate the enum with EnumClass::cases()
Solution
Wim
Wim12mo ago
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;
}
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;
}
Want results from more Discord servers?
Add your server