Filament Enum

In TextColumn I want to show case USD How can I? In text column show me us dollar.
case USD = 'us dollar';
case USD = 'us dollar';
9 Replies
xy
xy16mo ago
It will show value of enum case. You can also use HasLabel: https://filamentphp.com/docs/3.x/support/enums#enum-labels
Shaung Bhone
Shaung BhoneOP16mo ago
Yes I'm using hasLabel. I want to show USD like that
xy
xy16mo ago
You can use formatStateUsing then EnumClass:from()?
Dennis Koch
Dennis Koch16mo ago
And did you implement the getLabel() method as shown in the docs?
Shaung Bhone
Shaung BhoneOP16mo ago
Tables\Columns\TextColumn::make('currency'),
Tables\Columns\TextColumn::make('currency'),
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum Currency: string implements HasLabel
{
case MMK = 'myanmar kyat';
case USD = 'us dollar';
case EUR = 'euro';
case SGD = 'singapore dollar';
case THB = 'thai baht';

public function getLabel(): ?string
{
return match ($this) {
self::USD => 'US Dollar',
self::EUR => 'Euro',
self::SGD => 'Singapore Dollar',
self::THB => 'Thai Baht',
self::MMK => 'Myanmar Kyat',
default => 'Unknown currency',
};
}
}
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum Currency: string implements HasLabel
{
case MMK = 'myanmar kyat';
case USD = 'us dollar';
case EUR = 'euro';
case SGD = 'singapore dollar';
case THB = 'thai baht';

public function getLabel(): ?string
{
return match ($this) {
self::USD => 'US Dollar',
self::EUR => 'Euro',
self::SGD => 'Singapore Dollar',
self::THB => 'Thai Baht',
self::MMK => 'Myanmar Kyat',
default => 'Unknown currency',
};
}
}
krekas
krekas16mo ago
Have you casted your field in the model?
Shaung Bhone
Shaung BhoneOP16mo ago
no
krekas
krekas16mo ago
🤦‍♂️ that's what you get when you don't read docs
krekas
krekas16mo ago
Filament Daily
YouTube
Filament: Enum Classes to Re-Use and Auto-Style
If you have a set of key-value options in the forms and tables, you may use PHP Enum classes to avoid repeating those options. Official docs: https://filamentphp.com/docs/3.x/support/enums
Want results from more Discord servers?
Add your server