Brick Money
I am using a third party package for the money field. I have cast with get and set attributes accordingly. Storing and viewing the record is no problem, bur on trying to edit the record i get an error: Property type not supported in Livewire for property: [{"amount":"100000.00","currency":"KES"}]
For context:
<?php
namespace App\Models\HumanResource;
use App\Casts\MoneyCast;
use App\Enums\HumanResource\ParameterType;
use App\Enums\HumanResource\PayrollCategory;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Support\RawJs;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Wallo\FilamentSelectify\Components\ToggleButton;
class PayrollParameter extends Model
{
use HasFactory;
protected function casts(): array
{
return [
'default' => MoneyCast::class,
'parameter_type' => ParameterType::class,
'payroll_category' => PayrollCategory::class,
];
}
}
<?php
namespace App\Casts;
use App\Models\Configurations\Profile;
use Brick\Money\Money;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
class MoneyCast implements CastsAttributes
{
private function profile(): Profile
{
return Profile::find(1);
}
/
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return Money::of($value, $this->profile()->currency->abbr);
}
/
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
if (!$value instanceof Money) {
return $value;
}
return $value->getAmount()->toInt();
}
}
0 Replies