F
Filamentā€¢11mo ago
@nasilemak

How to display an enum's label rather than value in a TextColumn?

My Customer model has a priority column which stores any one of these values: 1, 2, 3.
class Customer extends Model
{
public const PRIORITY_SELECT = [
1 => 'Low',
2 => 'Medium',
3 => 'High',
];
}
class Customer extends Model
{
public const PRIORITY_SELECT = [
1 => 'Low',
2 => 'Medium',
3 => 'High',
];
}
How do I make TextColumn::make('priority') display the value instead of the key? Here's what I have. However I don't want this displayed as a description but rather as the value itself.
Tables\Columns\TextColumn::make('priority')
->badge()
->description(fn (Customer $record): string => Customer::PRIORITY_SELECT[$record->priority])
->color(fn (string $state): string => match ($state) {
'1' => 'gray',
'2' => 'warning',
'3' => 'success',
}),
Tables\Columns\TextColumn::make('priority')
->badge()
->description(fn (Customer $record): string => Customer::PRIORITY_SELECT[$record->priority])
->color(fn (string $state): string => match ($state) {
'1' => 'gray',
'2' => 'warning',
'3' => 'success',
}),
Solution:
Found the solution. ->formatStateUsing()
Jump to solution
9 Replies
krekas
krekasā€¢11mo ago
move to enums instead of const
Solution
@nasilemak
@nasilemakā€¢11mo ago
Found the solution. ->formatStateUsing()
@nasilemak
@nasilemakOPā€¢11mo ago
I'm starting to use Enums less and less lately. I haven't found a straight forward way of fetching labels or a random item when working with Enums without defining Traits.
<?php

namespace App\Enums;

enum Priority: integer
{
case LOW = 1;
case MEDIUM = 2;
case HIGH = 3;
}
<?php

namespace App\Enums;

enum Priority: integer
{
case LOW = 1;
case MEDIUM = 2;
case HIGH = 3;
}
krekas
krekasā€¢11mo ago
if you would use enum you could just pass it in the options()
krekas
krekasā€¢11mo ago
for the color there's also a method
@nasilemak
@nasilemakOPā€¢11mo ago
I hadn't realized Filament ships with a HasLabel interface! Been using my own HasLabel trait this whole time. Got to refactor a dozen projects now lol
krekas
krekasā€¢11mo ago
rtfm lol šŸ˜„
@nasilemak
@nasilemakOPā€¢11mo ago
RTFM will be my epitaph šŸŖ¦ lol Thanks again mate Ok refactored my code to use an enum instead. Got it working in the Form. But what about in the Table in the TextColumn?
use App\Enums\Priority;

class CustomerResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('priority')
->options(Priority::class),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('priority')
->badge(),
]);
}
}
use App\Enums\Priority;

class CustomerResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('priority')
->options(Priority::class),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('priority')
->badge(),
]);
}
}
never mind, forgot to cast my enum in my Eloquent model. Sorted. Thanks
Want results from more Discord servers?
Add your server