F
Filament2mo ago
rabol

TypeError on resource where column is an Enum

I have a User Model, and did like this: php artisan make:filament-resource UserResource -G
and when I try to view the users i get this:
Filament\Tables\Columns\TextColumn::Filament\Tables\Columns\Concerns\{closure}(): Return value must be of type ?string, App\Enums\UserLevelEnum returned
Filament\Tables\Columns\TextColumn::Filament\Tables\Columns\Concerns\{closure}(): Return value must be of type ?string, App\Enums\UserLevelEnum returned
I get it even if I remove the column that uses enum the enum:
enum UserLevelEnum: int implements HasLabel
{
---
}
enum UserLevelEnum: int implements HasLabel
{
---
}
What am I missing ?
Solution:
Why do you have numeric method on the TextColumn?
Jump to solution
17 Replies
Abi
Abi2mo ago
Do you have any casts defined on the User Model?
rabol
rabol2mo ago
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'user_level' => UserLevelEnum::class,
];
}
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'user_level' => UserLevelEnum::class,
];
}
Abi
Abi2mo ago
can you add the code of the enum class also here. If you use HasLabel on the enum, the text column should automatically use it. https://filamentphp.com/docs/3.x/support/enums#using-the-enum-label-with-a-text-column-in-your-table
rabol
rabol2mo ago
<?php

namespace App\Enums;

use App\Enums\Traits\EnumHelper;
use Filament\Support\Contracts\HasLabel;

enum UserLevelEnum: int implements HasLabel
{
use EnumHelper;

case None = 0;
case Free = 1;
case Standard = 10;
case Plus = 20;
case Pro = 30;
case Special = 40;
case Admin = 90;
case Unlimited = 999;

public function getLabel(): ?string
{
return match ($this) {
self::None => ('None'),
self::Free => ('Free'),
self::Standard => ('Standard'),
self::Plus => ('Plus'),
self::Pro => ('Pro'),
self::Special => ('Special'),
self::Admin => ('Admin'),
self::Unlimited => ('Unlimited'),
};
}
}
<?php

namespace App\Enums;

use App\Enums\Traits\EnumHelper;
use Filament\Support\Contracts\HasLabel;

enum UserLevelEnum: int implements HasLabel
{
use EnumHelper;

case None = 0;
case Free = 1;
case Standard = 10;
case Plus = 20;
case Pro = 30;
case Special = 40;
case Admin = 90;
case Unlimited = 999;

public function getLabel(): ?string
{
return match ($this) {
self::None => ('None'),
self::Free => ('Free'),
self::Standard => ('Standard'),
self::Plus => ('Plus'),
self::Pro => ('Pro'),
self::Special => ('Special'),
self::Admin => ('Admin'),
self::Unlimited => ('Unlimited'),
};
}
}
Abi
Abi2mo ago
Looks fine. OK, can you send me the TextColumn code on the resource that binds the user_level
rabol
rabol2mo ago
Tables\Columns\TextColumn::make('user_level') ->numeric() ->sortable(),
Abi
Abi2mo ago
not sure what the EnumHelper does, can you try removing that trait to see if that fixes.
Solution
Abi
Abi2mo ago
Why do you have numeric method on the TextColumn?
Abi
Abi2mo ago
can you remove that
rabol
rabol2mo ago
-G the make command made it like this
Abi
Abi2mo ago
did you generate the code before casting the column to UserLevel Enum? If so, the make command would use the type of the column on the DB to set that method. Remove that since you have a cast on the modal and that should do it
rabol
rabol2mo ago
no, code was created after - I'm trying to switch from Nova to Filament on this I just did a php artisan make:filament-resource UserResource -G --force and this is the outcome Tables\Columns\TextColumn::make('user_level') ->numeric() ->sortable(), so maybe a bug @Dan Harrin 🙂
Dan Harrin
Dan Harrin2mo ago
please open an issue with reproduction
Abi
Abi2mo ago
din't removing the ->numeric() method no help?
rabol
rabol2mo ago
yes, that helped but the 'bug' is that this is generated
Tables\Columns\TextColumn::make('user_level')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('user_level')
->numeric()
->sortable(),
by the command
php artisan make:filament-resource UserResource -G
php artisan make:filament-resource UserResource -G
Abi
Abi2mo ago
ok
rabol
rabol2mo ago
@Dan Harrin created a bug and a github repro to show the issue