CheckBoxList, Enums and TextColumn display conundrum

Salut

Interesting little problem. I have a Checkbox list on the form that is populated with an Enum. This all fine and works as expected - the labels are displayed and the values are stored correctly:

CheckboxList::make('type')
   ->label('Type')
   ->options(OrganisationTypes::class)
   ->columns(2)
   ->required(),

On the table I want to display the the selected values

TextColumn::make('type'),

Which is where the problem arises - the column displays the stored value and not the label.

Because this is a multi-value field , stored as JSON in the model I need to cast it to type => 'array'
If this were a simple/single value field (e.g CheckBox or Select) then I could cast it to the enum type => OrganisationTypes::class and the correct label would be displayed in the table column.

So how do I cast the field to an array and still have the Enum return the correct labels in the table column?

Thanks for reading this far!

My enum for reference
enum OrganisationTypes: string implements HasLabel
{
    case CUSTOMER = 'customer';
    case SUPPLIER = 'supplier';
    case MANUFACTURER = 'manufacturer';
    case WAREHOUSE = 'warehouse';
    case LOGISTICS = 'logictics';

    public function getLabel(): ?string
    {
        return match ($this) {
            self::CUSTOMER => 'Customer',
            self::SUPPLIER => 'Supplier',
            self::MANUFACTURER => 'Manufacturer',
            self::WAREHOUSE => 'Warehouse',
            self::LOGISTICS => 'Logistics',
            default => '',
        };
    }
}
Solution
Try https://filamentphp.com/docs/3.x/forms/advanced#field-hydration

Something like below should work. not tested code

->formatStateUsing(function (string $state){
$Labels = [];
foreach($state as $EnumValue){
$Labels[] = OrganisationTypes::from($EnumValue)->getLabel()
}
return implode(',',$Labels);
})
Was this page helpful?