F
Filament7mo 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
Abi7mo ago
Do you have any casts defined on the User Model?
rabol
rabolOP7mo 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
Abi7mo 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
rabolOP7mo 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
Abi7mo ago
Looks fine. OK, can you send me the TextColumn code on the resource that binds the user_level
rabol
rabolOP7mo ago
Tables\Columns\TextColumn::make('user_level') ->numeric() ->sortable(),
Abi
Abi7mo ago
not sure what the EnumHelper does, can you try removing that trait to see if that fixes.
Solution
Abi
Abi7mo ago
Why do you have numeric method on the TextColumn?
Abi
Abi7mo ago
can you remove that
rabol
rabolOP7mo ago
-G the make command made it like this
Abi
Abi7mo 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
rabolOP7mo 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 Harrin7mo ago
please open an issue with reproduction
Abi
Abi7mo ago
din't removing the ->numeric() method no help?
rabol
rabolOP7mo 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
Abi7mo ago
ok
rabol
rabolOP7mo ago
@Dan Harrin created a bug and a github repro to show the issue
Want results from more Discord servers?
Add your server