how to use table ->defaultGroup with enum field?

I want to use the new v3 table group feature with a column casted to PHP enum, but seem unable, what are some ways I can do it? Code is something like $table->defaultGroup('status') where status is casted to enum
6 Replies
pocket.racer
pocket.racerOP16mo ago
bump Some code example in my ListOrders As you can see, I do $record->status->value because my status is casted to a PHP Enum
public function table(Table $table): Table
{
return parent::table($table)
->defaultSort('id', 'desc')
->groups([
'created_at',
Group::make('status')
->getTitleFromRecordUsing(fn (Order $record): string => $record->status->value),
]);
//->defaultGroup('status')
}
public function table(Table $table): Table
{
return parent::table($table)
->defaultSort('id', 'desc')
->groups([
'created_at',
Group::make('status')
->getTitleFromRecordUsing(fn (Order $record): string => $record->status->value),
]);
//->defaultGroup('status')
}
having status as default group crashes, if i choose status as the group by choice, it crashes too Error message is Object of class App\Enums\OrderStatusEnum could not be converted to string
Filament
Filament16mo ago
We need more information to help you debug your problem. Please click on the top left 'SHARE' button of the error page you're seeing and share the link with us.
pocket.racer
pocket.racerOP16mo ago
I dig deeper, the error is thrown at strval()
No description
pocket.racer
pocket.racerOP16mo ago
i think because status is casted to php enum in my model, strval($order->status) throw that error. It doesn't matter that I specified ->getTitleFromRecordUsing(fn (Order $record): string => $record->status->value), The code does not reach that stage, it already fail at the Group::make('status') stage
pocket.racer
pocket.racerOP16mo ago
So similarly i think the example in documentation will fail too. because if status does not pass strval() in Group::make('key') it crashes
No description
pocket.racer
pocket.racerOP16mo ago
i have also tried the following but same enum cannot convert to string error too. also at strval
use Illuminate\Contracts\Database\Query\Builder;

Group::make('status')
->groupQueryUsing(fn (Builder $query) => $query->groupBy('status'))
use Illuminate\Contracts\Database\Query\Builder;

Group::make('status')
->groupQueryUsing(fn (Builder $query) => $query->groupBy('status'))
i think fix will be if ($key instanceof Enum [is it backed enum or something else] ) { $key = $key->value; } in the getKey() method Hope it helps. Somebody can make a PR to filament table group to add this condition, im too shy to reveal my real name (& lazy to create an alternate github account). So i would just not use this feature on my site but if it affect u, u can make the fix i suggest above as a pr to filament, thanks!
Want results from more Discord servers?
Add your server