How can I replace enum of Badge column?

I know that the documentation saus that it is removed, but in v2 I used enum with the Settings page. How do I approach this now?
public static function table(Table $table): Table
{
return $table
->columns([
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
->enum([
app(StatusSettings::class)->time_registration_status_pending => TimeRegistrationStatus::find(app(StatusSettings::class)->time_registration_status_pending)->name,
app(StatusSettings::class)->time_registration_status_written_off => TimeRegistrationStatus::find(app(StatusSettings::class)->time_registration_status_written_off)->name,
// ... (more statuses)
])
])
public static function table(Table $table): Table
{
return $table
->columns([
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
->enum([
app(StatusSettings::class)->time_registration_status_pending => TimeRegistrationStatus::find(app(StatusSettings::class)->time_registration_status_pending)->name,
app(StatusSettings::class)->time_registration_status_written_off => TimeRegistrationStatus::find(app(StatusSettings::class)->time_registration_status_written_off)->name,
// ... (more statuses)
])
])
22 Replies
ChesterS
ChesterS8mo ago
Something like
TextColumn::make('status_id')

->badge()
TextColumn::make('status_id')

->badge()
in your Status enum you do
enum Status: string implements HasColor
{

public function getColor(): string
{
return match ($this) {
Status::New => 'danger',
Status::Accepted => 'warning',
Status::Completed => 'success',
default => 'info'
};
}
}
enum Status: string implements HasColor
{

public function getColor(): string
{
return match ($this) {
Status::New => 'danger',
Status::Accepted => 'warning',
Status::Completed => 'success',
default => 'info'
};
}
}
You can do the same with the label Make sure you have cast your field in your model. https://filamentphp.com/docs/3.x/support/enums
Dennis Koch
Dennis Koch8mo ago
The upgrade guide says:
BadgeColumn::enum() removed You can use a formatStateUsing() function to transform text.
Also the above mentioned way should work
Matthew
Matthew8mo ago
Hello, Ive tried this but I have gotten it to work yet. Im using SettingsPage as well, and I think that complicates things a little bit. How do I specify the enum() from v2 to the enum class?
<?php
use Filament\Support\Contracts\HasLabel;

enum TimeRegistrationBadgeEnum: string implements HasLabel
{
case time_registration_status_pending = 'pending';
case time_registration_status_approved = 'approved';
case time_registration_status_refused = 'refused';
case time_registration_status_written_off = 'Written Off';
// public $time_registration_status_pending;
// public $time_registration_status_approved;
// public $time_registration_status_refused;
// public $time_registration_status_written_off;
public function getLabel(): ?string
{
return $this->name;

// or

// return match ($this) {
// self::Draft => 'Draft',
// self::Reviewing => 'Reviewing',
// self::Published => 'Published',
// self::Rejected => 'Rejected',
// };
}
}
<?php
use Filament\Support\Contracts\HasLabel;

enum TimeRegistrationBadgeEnum: string implements HasLabel
{
case time_registration_status_pending = 'pending';
case time_registration_status_approved = 'approved';
case time_registration_status_refused = 'refused';
case time_registration_status_written_off = 'Written Off';
// public $time_registration_status_pending;
// public $time_registration_status_approved;
// public $time_registration_status_refused;
// public $time_registration_status_written_off;
public function getLabel(): ?string
{
return $this->name;

// or

// return match ($this) {
// self::Draft => 'Draft',
// self::Reviewing => 'Reviewing',
// self::Published => 'Published',
// self::Rejected => 'Rejected',
// };
}
}
Yeah, I mentioned that I saw the comment that it was removed, but now im not sure how I can implement it in the class
ChesterS
ChesterS8mo ago
<?php
enum TimeRegistrationBadgeEnum: string implements HasLabel
{
case time_registration_status_pending = 'pending';
case time_registration_status_approved = 'approved';
case time_registration_status_refused = 'refused';
case time_registration_status_written_off = 'Written Off';

public function getLabel(): ?string
{
return match ($this) {
self::time_registration_status_pending => 'Pending',
self::time_registration_status_approved => 'Approved',
self::time_registration_status_refused => 'Refused',
self::time_registration_status_written_off => 'Written off',
};
}
}
<?php
enum TimeRegistrationBadgeEnum: string implements HasLabel
{
case time_registration_status_pending = 'pending';
case time_registration_status_approved = 'approved';
case time_registration_status_refused = 'refused';
case time_registration_status_written_off = 'Written Off';

public function getLabel(): ?string
{
return match ($this) {
self::time_registration_status_pending => 'Pending',
self::time_registration_status_approved => 'Approved',
self::time_registration_status_refused => 'Refused',
self::time_registration_status_written_off => 'Written off',
};
}
}
I would suggest you read a bit about enums though It can be much nicer if you change it to something like
enum TimeRegistrationStatus: string implements HasLabel
{
case Pending = 'pending';
case Approved = 'approved';
case Refused = 'refused';
case WrittenOff = 'written_off';

public function getLabel(): ?string
{
return match ($this) {
self::Pending => 'Pending',
self::Approved => 'Approved',
self::Refused => 'Refused',
self::WrittenOff => 'Written off',
};
}
}
enum TimeRegistrationStatus: string implements HasLabel
{
case Pending = 'pending';
case Approved = 'approved';
case Refused = 'refused';
case WrittenOff = 'written_off';

public function getLabel(): ?string
{
return match ($this) {
self::Pending => 'Pending',
self::Approved => 'Approved',
self::Refused => 'Refused',
self::WrittenOff => 'Written off',
};
}
}
Also, make sure you cast it in your model
// Just an example
protected $casts = [
'registration_status' => TimeRegistrationStatus::class,
];
// Just an example
protected $casts = [
'registration_status' => TimeRegistrationStatus::class,
];
Matthew
Matthew8mo ago
Hello! With this, I only get the id of the status
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
->formatStateUsing(fn (string $state): string => __("state"))
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
->formatStateUsing(fn (string $state): string => __("state"))
Matthew
Matthew8mo ago
No description
Matthew
Matthew8mo ago
Ah, I see now, I change it to
->formatStateUsing(fn (string $state): string => __(TimeRegistrationStatus::find($state)->name))
->formatStateUsing(fn (string $state): string => __(TimeRegistrationStatus::find($state)->name))
Dennis Koch
Dennis Koch8mo ago
The message literally said: „it was removed, use this“ 🤔
ChesterS
ChesterS8mo ago
@Matthew Please actually read the documentation here https://filamentphp.com/docs/3.x/support/enums It also handles showing different colors based on the enum value. If you follow the directions, your end result will be much simpler And if you want the text to be translated, translate it in the getLabel() method
Matthew
Matthew8mo ago
Thanks, I will look further into it. Ive never worked with enums before Hello again. I followed this approach. However, this gives me the wrong results. Instead of getting pending, I get approved.
->formatStateUsing(function (string $state) {
// Create an instance of the enum to call getLabel()
$enumInstance = TimeRegistrationBadgeEnum::cases()[$state] ?? null;
// dd($enumInstance);
return $enumInstance->getLabel();
})
->formatStateUsing(function (string $state) {
// Create an instance of the enum to call getLabel()
$enumInstance = TimeRegistrationBadgeEnum::cases()[$state] ?? null;
// dd($enumInstance);
return $enumInstance->getLabel();
})
Im not sure I understand what Im supposed to do. I read the docs, but I dont really get it still
Dennis Koch
Dennis Koch8mo ago
If you have an enum and a cast for that enum you can entirely remove formatStateUsing
ChesterS
ChesterS8mo ago
Have you made any changes to your enum and form? How do they look like now? (the code)
Matthew
Matthew8mo ago
I havent made any changes, because they all led to errors, or wrong output I will share again
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
->formatStateUsing(function (string $state) {
$enumInstance = TimeRegistrationStatus::find($state)->name ?? null;
return $enumInstance;
})
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
->formatStateUsing(function (string $state) {
$enumInstance = TimeRegistrationStatus::find($state)->name ?? null;
return $enumInstance;
})
<?php
namespace App\Enums;
use Filament\Support\Contracts\HasLabel;

enum TimeRegistrationBadgeEnum: string implements HasLabel
{
case time_registration_status_pending = 'pending';
case time_registration_status_approved = 'approved';
case time_registration_status_refused = 'refused';
case time_registration_status_written_off = 'Written Off';

public function getLabel(): ?string
{
return match ($this) {
self::time_registration_status_pending => 'Pending',
self::time_registration_status_approved => 'Approved',
self::time_registration_status_refused => 'Refused',
self::time_registration_status_written_off => 'Written off',
};
}
}
<?php
namespace App\Enums;
use Filament\Support\Contracts\HasLabel;

enum TimeRegistrationBadgeEnum: string implements HasLabel
{
case time_registration_status_pending = 'pending';
case time_registration_status_approved = 'approved';
case time_registration_status_refused = 'refused';
case time_registration_status_written_off = 'Written Off';

public function getLabel(): ?string
{
return match ($this) {
self::time_registration_status_pending => 'Pending',
self::time_registration_status_approved => 'Approved',
self::time_registration_status_refused => 'Refused',
self::time_registration_status_written_off => 'Written off',
};
}
}
for some reason it seems unnecessarily complicated 😅
Dennis Koch
Dennis Koch8mo ago
Does status_id have a cast to enum?
Matthew
Matthew8mo ago
it does not
Dennis Koch
Dennis Koch8mo ago
Why not? That’s exactly what I told you in the last message
Matthew
Matthew8mo ago
Dont I need to do something like this?
No description
Dennis Koch
Dennis Koch8mo ago
No. There is a cast for Enums. Check the Laravel docs
Matthew
Matthew8mo ago
No description
Dennis Koch
Dennis Koch8mo ago
Yes
Matthew
Matthew8mo ago
Im getting error: "1" is not a valid backing value for enum App\Enums\TimeRegistrationBadgeEnum
public static function canEditTableRow($record) {

return @TimeRegistration::find($record->id)->status_id != app(StatusSettings::class)->time_registration_status_pending;

}
public static function canEditTableRow($record) {

return @TimeRegistration::find($record->id)->status_id != app(StatusSettings::class)->time_registration_status_pending;

}
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
BadgeColumn::make('status_id')->label('Status')->searchable()->toggleable(isToggledHiddenByDefault: false)
->colors([
'primary' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_pending,
'danger' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_refused,
'success' => static fn ($state): bool => $state == app(StatusSettings::class)->time_registration_status_approved,
])
ChesterS
ChesterS8mo ago
You have done literally none of the things suggested here Still using BadgeColumn Not followed either the instructions in the documentation or suggestions posted here (like casting to enum) You must read about enums - the error you're getting ("1" is not a valid backing value for enum App\Enums\TimeRegistrationBadgeEnum) is probably because your value from the DB doesn't match the enum values. Here is your final solution
TextColumn::make('status_id')->label('Status')
->searchable()
->toggleable(isToggledHiddenByDefault: false)
->badge()
TextColumn::make('status_id')->label('Status')
->searchable()
->toggleable(isToggledHiddenByDefault: false)
->badge()
If you do the rest of the steps as described, this will work. Good luck