I'm upgrading my Laravel / Filament app from v2 to v3 (with Laravel 11, mysql, PHP 8.3).
Now I'm getting this error:
Property type not supported in Livewire for property: ["0.0000"]
The problem seems to be my Laravel Custom Cast of decimal type (As I use the php-decimal extension https://php-decimal.github.io):
Project Model, with an example of a failing column:
protected function casts(): array
{
return ['investment' => DecimalZeroCast::class,];
}
which is defined as this
class DecimalZeroCast implements CastsAttributes
{
/**
* Cast the given value.
*/
public function get(Model $model, string $key, mixed $value, array $attributes): Decimal
{
return is_null($value) ? new Decimal(0) : (is_float($value) ? new Decimal((string) $value) : new Decimal($value));
}
/**
* Prepare the given value for storage.
*/
public function set(Model $model, string $key, mixed $value, array $attributes): float
{
return is_null($value) ? 0 : ($value instanceof Decimal ? $value->toFixed(12) : $value);
}
}
In the mysql database the column is defined as decimal(24,12).
I've already found out, that this is a Livewire thing, that Livewire can not handle complex types: https://github.com/filamentphp/filament/issues/7465
What I found out:
Dan Harrin's answer on github pointed me to the right direction https://github.com/filamentphp/filament/discussions/3490 to use mutate hooks https://filamentphp.com/docs/3.x/panels/resources/editing-records
My solution is to mutate the decimal to a float in the hook where the Filament Form is filled:
EditProject.php
protected function mutateFormDataBeforeFill(array $data): array
{
$data['investment'] = $data['investment']->toFloat();
return $data;
}