F
Filament6mo ago
Cushty

enums with different colors

Hi I've added some code to change the color dependent on the enum value, but I can't get it to work, it always shows the danger color, hope you can help, thanks
Tables\Columns\TextColumn::make('status')
->badge()
->colors([
'success' => fn ($state) => $state === ApplicationStatusEnum::APPROVED->value,
'danger' => fn ($state) => $state === ApplicationStatusEnum::DENIED->value,
'warning' => fn ($state) => $state === ApplicationStatusEnum::PENDING->value,

])
Tables\Columns\TextColumn::make('status')
->badge()
->colors([
'success' => fn ($state) => $state === ApplicationStatusEnum::APPROVED->value,
'danger' => fn ($state) => $state === ApplicationStatusEnum::DENIED->value,
'warning' => fn ($state) => $state === ApplicationStatusEnum::PENDING->value,

])
Solution:
i fixed it, I added this to my enum file, and it made it so much easier all I needed was the ->badge on the resource and it picked up the colors. just in case someone runs into the issue ```...
Jump to solution
1 Reply
Solution
Cushty
Cushty6mo ago
i fixed it, I added this to my enum file, and it made it so much easier all I needed was the ->badge on the resource and it picked up the colors. just in case someone runs into the issue
use Filament\Support\Contracts\HasColor;

enum ApplicationStatusEnum: string implements HasColor
{
case PENDING = 'Pending';
case APPROVED = 'Approved';
case DENIED = 'Denied';



public function getColor(): string|array|null
{
return match ($this) {
self::PENDING => 'warning',
self::APPROVED => 'success',
self::DENIED => 'danger',
};
}
}
use Filament\Support\Contracts\HasColor;

enum ApplicationStatusEnum: string implements HasColor
{
case PENDING = 'Pending';
case APPROVED = 'Approved';
case DENIED = 'Denied';



public function getColor(): string|array|null
{
return match ($this) {
self::PENDING => 'warning',
self::APPROVED => 'success',
self::DENIED => 'danger',
};
}
}
Want results from more Discord servers?
Add your server