How can I use Laravel ENUM in the Filement table?

how can I use Laravel's ENUM in the Filement table and form? I'm doing it this way, but is there a better way to do it?

In Form:
Forms\Components\Select::make('status')
->label('Status')
->options([
'awaiting_evaluation' => ServiceStatus::AwaitingEvaluation->label(),

'approved' =>
ServiceStatus::Approved->label(),

'awaiting_payment' =>
ServiceStatus::AwaitingPayment->label(),

'device_collected' =>
ServiceStatus::DeviceCollected->label(),
])
->default('awaiting_evaluation')
->required(),

In Table:
Tables\Columns\SelectColumn::make('status')
->label('Status')
->options([
'awaiting_evaluation' => ServiceStatus::AwaitingEvaluation->label(),

'approved' =>
ServiceStatus::Approved->label(),

'awaiting_payment' =>
ServiceStatus::AwaitingPayment->label(),

'device_collected' =>
ServiceStatus::DeviceCollected->label(),
])
->selectablePlaceholder(false)
->sortable()
->searchable(),

In Form:
Forms\Components\Select::make('status')
->label('Status')
->options([
'awaiting_evaluation' => ServiceStatus::AwaitingEvaluation->label(),

'approved' =>
ServiceStatus::Approved->label(),

'awaiting_payment' =>
ServiceStatus::AwaitingPayment->label(),

'device_collected' =>
ServiceStatus::DeviceCollected->label(),
])
->default('awaiting_evaluation')
->required(),

In Table:
Tables\Columns\SelectColumn::make('status')
->label('Status')
->options([
'awaiting_evaluation' => ServiceStatus::AwaitingEvaluation->label(),

'approved' =>
ServiceStatus::Approved->label(),

'awaiting_payment' =>
ServiceStatus::AwaitingPayment->label(),

'device_collected' =>
ServiceStatus::DeviceCollected->label(),
])
->selectablePlaceholder(false)
->sortable()
->searchable(),
3 Replies
Esi
Esi2mo ago
You should do something like this:
enum OrderPaymentMethod: string implements HasColor, HasIcon, HasLabel
{
case Card = 'card';

case Cash = 'cash';

public function getLabel(): string
{
return match ($this) {
self::Card => 'Credit card',
self::Cash => 'Cash on delivery',
};
}

public function getColor(): string | array | null
{
return match ($this) {
self::Card => 'success',
self::Cash => 'warning',
};
}

public function getIcon(): ?string
{
return match ($this) {
self::Card => 'heroicon-m-credit-card',
self::Cash => 'heroicon-o-wallet',
};
}
}
enum OrderPaymentMethod: string implements HasColor, HasIcon, HasLabel
{
case Card = 'card';

case Cash = 'cash';

public function getLabel(): string
{
return match ($this) {
self::Card => 'Credit card',
self::Cash => 'Cash on delivery',
};
}

public function getColor(): string | array | null
{
return match ($this) {
self::Card => 'success',
self::Cash => 'warning',
};
}

public function getIcon(): ?string
{
return match ($this) {
self::Card => 'heroicon-m-credit-card',
self::Cash => 'heroicon-o-wallet',
};
}
}
and then in the table ->options(OrderPaymentMethod::class)
Want results from more Discord servers?
Add your server