F
Filament2mo ago
bearer

How to export table with enum column?

Hello! I have column that maps to some enum. When I try to use formatStateUsing:
ExportColumn::make('lang')
->formatStateUsing(function (LanguageCode $code) {
return $code->value;
}),
ExportColumn::make('lang')
->formatStateUsing(function (LanguageCode $code) {
return $code->value;
}),
I receive error
[2024-04-22 22:36:01] local.ERROR: Target [App\Enums\LanguageCode] is not instantiable while building [Laravel\Horizon\Console\WorkCommand, Filament\Actions\Exports\Jobs\ExportCsv]. {"userId":1,"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target [App\\Enums\\LanguageCode] is not instantiable while building [Laravel\\Horizon\\Console\\WorkCommand, Filament\\Actions\\Exports\\Jobs\\ExportCsv]
[2024-04-22 22:36:01] local.ERROR: Target [App\Enums\LanguageCode] is not instantiable while building [Laravel\Horizon\Console\WorkCommand, Filament\Actions\Exports\Jobs\ExportCsv]. {"userId":1,"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target [App\\Enums\\LanguageCode] is not instantiable while building [Laravel\\Horizon\\Console\\WorkCommand, Filament\\Actions\\Exports\\Jobs\\ExportCsv]
Is it possivle to fix it?
6 Replies
awcodes
awcodes2mo ago
Can you not cast the lang attribute on the model as the enum?
bearer
bearer2mo ago
I can, but it's not convenient in other places. Anyway I can use state() and then just get what I want, but I think there should be some way with formatStateUsing
awcodes
awcodes2mo ago
If the enum doesn’t work in all places, then I kinda feel it misses the point of using an enum. The whole point of an enum is to normalize the value or case across the app.
bearer
bearer2mo ago
It's what I do. Values in DB are the same as in the my enum
awcodes
awcodes2mo ago
As they should be. That’s the point of the cast. What I’m getting at is that the enum shouldn’t be more or less complicated depending on where it is accessed. And if it is, something in the app is not using the enum in the way it is supposed to be used. Of course I’m saying this from a fundamental pov, I have no insight into your actual app logic or use case based on what’s been provided. 🙂
fagner
fagner2mo ago
Hi i had the same problem and i solved it this way:
protected $casts = [
'status' => DoacaoStatusPagamento::class
];

protected $appends = [
'new_status'
];

public function getNewStatusAttribute(): string
{
return $this->status->getLabel();
}
protected $casts = [
'status' => DoacaoStatusPagamento::class
];

protected $appends = [
'new_status'
];

public function getNewStatusAttribute(): string
{
return $this->status->getLabel();
}
``
Want results from more Discord servers?
Add your server
More Posts