Why are my enum labels not being used in my table column?

I've got this enum class
namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum ApprovalType: string implements HasLabel
{
case MILESTONE = 'milestone';
case FINAL = 'final';

public function getLabel(): ?string
{
return match ($this) {
self::MILESTONE => 'Milestone Approval',
self::FINAL => 'Final Sign-Off',
};
}
}
namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum ApprovalType: string implements HasLabel
{
case MILESTONE = 'milestone';
case FINAL = 'final';

public function getLabel(): ?string
{
return match ($this) {
self::MILESTONE => 'Milestone Approval',
self::FINAL => 'Final Sign-Off',
};
}
}
It's cast in the model...
namespace App\Models;

use App\Enums\ApprovalType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class JobApproval extends Model
{
protected function casts(): array
{
return [
'type' => ApprovalType::class,
];
}

public function job(): BelongsTo
{
return $this->belongsTo(Job::class);
}

public function approver(): BelongsTo
{
return $this->belongsTo(Contact::class);
}
}
namespace App\Models;

use App\Enums\ApprovalType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class JobApproval extends Model
{
protected function casts(): array
{
return [
'type' => ApprovalType::class,
];
}

public function job(): BelongsTo
{
return $this->belongsTo(Job::class);
}

public function approver(): BelongsTo
{
return $this->belongsTo(Contact::class);
}
}
And my column is...
Tables\Columns\TextColumn::make('type'),
Tables\Columns\TextColumn::make('type'),
I must have missed something simple, but I have no idea what.
Solution:
what about $casts instead of method? ```php protected $casts = [ 'type' => ApprovalType::class,...
Jump to solution
12 Replies
Alan
Alan2mo ago
Hello try something like:
->formatStateUsing(fn($state) => $state instanceof ApprovalType ? $state->getLabel() : $state)
->formatStateUsing(fn($state) => $state instanceof ApprovalType ? $state->getLabel() : $state)
awcodes
awcodes2mo ago
This looks ok to me and I would expect it to work.
Vp
Vp2mo ago
I got this issue on beta (iirc) but now I use latest version v4.0.4 and it's fixed. Not sure which version fix this but it works like expected now
Glenn Jacobs
Glenn JacobsOP2mo ago
Me too, hence my confusion I'm still on v3 for the moment
LeandroFerreira
LeandroFerreira2mo ago
what is showing in the TextColumn?
Glenn Jacobs
Glenn JacobsOP2mo ago
final (ie. the raw value, not the label)
LeandroFerreira
LeandroFerreira2mo ago
Your code is right. Do you have other enums that are working? Any customization in appserviceprovider?
php artisan tinker

App\Models\JobApproval::first()->type;
php artisan tinker

App\Models\JobApproval::first()->type;
what is the output?
Glenn Jacobs
Glenn JacobsOP2mo ago
"final"
Solution
LeandroFerreira
LeandroFerreira2mo ago
what about $casts instead of method?
protected $casts = [
'type' => ApprovalType::class,
];
protected $casts = [
'type' => ApprovalType::class,
];
Glenn Jacobs
Glenn JacobsOP2mo ago
OMG!! Legend
LeandroFerreira
LeandroFerreira2mo ago
Laravel version, maybe?
Glenn Jacobs
Glenn JacobsOP2mo ago
I think you're right. I must have been using the new syntax when it wasn't available. Well, I hope this thread helps someone else's sanity 🙂 Many thanks again

Did you find this page helpful?