Using the enum label with a text column in my table.

If you use a TextColumn with the Table Builder, and it is cast to an enum in your Eloquent model, Filament will automatically use the HasLabel interface to display the enum's label instead of its raw value.
If you use a TextColumn with the Table Builder, and it is cast to an enum in your Eloquent model, Filament will automatically use the HasLabel interface to display the enum's label instead of its raw value.
I read the doc. That not work for me. Where am I do wrong? CompanyResource.php
Tables\Columns\TextColumn::make('type'),
Tables\Columns\TextColumn::make('type'),
Company.php
protected $casts = [
'type' => EmploymentType::class,
];
protected $casts = [
'type' => EmploymentType::class,
];
Solution:
I don't have cast in my Model.
Jump to solution
24 Replies
Lara Zeus
Lara Zeus14mo ago
did you add HasLabel to EmploymentType
Shaung Bhone
Shaung BhoneOP14mo ago
Yes
cheesegrits
cheesegrits14mo ago
Are you sure you added the 'implements HasLabel' to the enum declaration, as well as adding the HasLabel method?
Shaung Bhone
Shaung BhoneOP14mo ago
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum EmploymentType: string implements HasLabel
{
case FULL_TIME = 'full time';
case PART_TIME = 'part time';
case REMOTE = 'remote';
case CONTRACT = 'contract';
case INTERNSHIP = 'internship';
case VOLUNTEER = 'volunteer';
case HYBRID = 'hybrid';

public function getLabel(): ?string
{
return match ($this) {
self::FULL_TIME => 'Full-Time',
self::PART_TIME => 'Part-Time',
self::REMOTE => 'Remote',
self::CONTRACT => 'Contract',
self::INTERNSHIP => 'Internship',
self::VOLUNTEER => 'Volunteer',
self::HYBRID => 'Hybrid',
default => '',
};
}
}
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum EmploymentType: string implements HasLabel
{
case FULL_TIME = 'full time';
case PART_TIME = 'part time';
case REMOTE = 'remote';
case CONTRACT = 'contract';
case INTERNSHIP = 'internship';
case VOLUNTEER = 'volunteer';
case HYBRID = 'hybrid';

public function getLabel(): ?string
{
return match ($this) {
self::FULL_TIME => 'Full-Time',
self::PART_TIME => 'Part-Time',
self::REMOTE => 'Remote',
self::CONTRACT => 'Contract',
self::INTERNSHIP => 'Internship',
self::VOLUNTEER => 'Volunteer',
self::HYBRID => 'Hybrid',
default => '',
};
}
}
Lara Zeus
Lara Zeus14mo ago
if you could change the name from type not sure
LeandroFerreira
LeandroFerreira14mo ago
This was supposed to work... What is the output on the table?
Shaung Bhone
Shaung BhoneOP14mo ago
key!. not label.
LeandroFerreira
LeandroFerreira14mo ago
is the 'full time' a key?
Shaung Bhone
Shaung BhoneOP14mo ago
No description
LeandroFerreira
LeandroFerreira14mo ago
should work. Could you share the project on github?
Dennis Koch
Dennis Koch14mo ago
I think you still need to pass ->enum(EmploymentType::class)?
LeandroFerreira
LeandroFerreira14mo ago
No description
Dennis Koch
Dennis Koch14mo ago
Alright. Was just guessing. On mobile right now 😅
ChesterS
ChesterS14mo ago
@Shaung Bhone Ok this is a longshot but can you rename (or make a copy of) the enum column to something other than type (eg employment_type ) and try again? Also, do other types have the same issue? eg if you change full time to contract, does it still not work?
Shaung Bhone
Shaung BhoneOP14mo ago
I don't think so, as you can see currency that is not showing label. SelectColumn is working. TextColumn is not work.
awcodes
awcodes14mo ago
Only thing that looks odd to me is the underscores in the case names. Can you try it with pascal case instead?
LeandroFerreira
LeandroFerreira14mo ago
I would like to try this repo because this doesn't make sense for me 😅
cheesegrits
cheesegrits14mo ago
Yeah, I've dug pretty deep into the code, created a test case on the Demo app, and I can't see how it fails. If the $state of that column is an instance of the enum with the GetLabel interface, Filament will call getLabel() on it. And the $state is just fetched from the $record returned from the Eloquent query. So for whatever reason, that $state is not the EmploymentType enum class when it reaches Filament. So I think the problem is elsewhere in Shaung's app, but I have no idea where.
Shaung Bhone
Shaung BhoneOP14mo ago
Thank you all let me try again and I got still stuck I will DM you.
cheesegrits
cheesegrits14mo ago
Just FYI, my PR to add an Enum example to the Demo app has been merged, so you can use it as a sanity check if you want. Just install / update the demo, and there's a working example of column badges with colors, and a Select. https://github.com/filamentphp/demo/commit/f2a27fd03ba8a8d2c2b1287d6b3949a456e47be7
Shaung Bhone
Shaung BhoneOP14mo ago
Thank you
Solution
Shaung Bhone
Shaung Bhone14mo ago
I don't have cast in my Model.
cheesegrits
cheesegrits14mo ago
Your OP shows a cast for it?
Shaung Bhone
Shaung BhoneOP14mo ago
Yes Wrong model Lol My bad
Want results from more Discord servers?
Add your server