How to covert enum value on Export Column?

There is an upgrade guide but doesn't show on the enum values part Error: Filament\Actions\Exports\ExportColumn::getFormattedState(): Return value must be of type ?string, App\Enums\Company Exporter column
ExportColumn::make('company')
->formatStateUsing(fn ($state) => $state?->label())
->label('Company'),
ExportColumn::make('company')
->formatStateUsing(fn ($state) => $state?->label())
->label('Company'),
Enums
<?php

namespace App\Enums;

use App\Enums\Traits\EnumToArray;

enum Company: string
{
use EnumToArray;

case Aventus = 'aventus';
case Avega = 'avega';
case HeadOffice = 'head_office';
case Makati = 'makati';
case Regional = 'regional';

public function label(): string
{
return match ($this) {
self::Aventus => 'Aventus',
self::Avega => 'Avega',
self::HeadOffice => 'Head Office',
self::Makati => 'Makati',
self::Regional => 'Regional Office',
};
}
}
<?php

namespace App\Enums;

use App\Enums\Traits\EnumToArray;

enum Company: string
{
use EnumToArray;

case Aventus = 'aventus';
case Avega = 'avega';
case HeadOffice = 'head_office';
case Makati = 'makati';
case Regional = 'regional';

public function label(): string
{
return match ($this) {
self::Aventus => 'Aventus',
self::Avega => 'Avega',
self::HeadOffice => 'Head Office',
self::Makati => 'Makati',
self::Regional => 'Regional Office',
};
}
}
2 Replies
igorclauss
igorclauss2mo ago
You can find all about enums in filament apps in the documentation: https://filamentphp.com/docs/4.x/advanced/enums
Jerome V
Jerome VOP2mo ago
I already know the enums.. in the v3 I do somehting like this ExportColumn::make('type') ->formatStateUsing(fn ($state) => $state instanceof \App\Services\Points\Enums\Type ? $state->labelAdmin() : ucfirst((string) $state)), but in the v4 it's not working anymore.. and in the import also this not working ImportColumn::make('mobile') ->requiredMapping() ->rules([ 'nullable', 'phone:PH', 'max:255', ]) ->fillRecordUsing(function ($record, $state) { if (empty($state)) { $record->mobile = null; return; } $record->mobile = (new PhoneNumber($state, 'PH'))->formatE164(); }), wasted time.. fixed by restarting queue

Did you find this page helpful?