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();
}
}
1 Reply
Dont cast to money, use an Accessor and a Mutator use the accessorr for table and infolist and the normal attribute to edit/create, in order to reduce code use a trait like this one <?php
namespace App\Models\Traits;
use Brick\Math\RoundingMode;
use Brick\Money\Money;
trait HasMoneyAttributes
{
/
* Accessor genérico para obtener el valor monetario.
*
* @param string $attribute
* @return Money
*/
public function getMoneyInstance(string $attribute): Money
{
$amountInCents = $this->attributes[$attribute] ?? 0;
// Verifica si el modelo tiene la relación 'currency' y si existe el código
$currencyCode = $this->currency->code ?? config('app.currency');
return Money::ofMinor($amountInCents, $currencyCode, null, RoundingMode::UP);
}
/
* Mutator genérico para almacenar dinero en centavos.
*
* @param string $attribute
* @param mixed $value
*/
protected function setMoneyAttribute(string $attribute, $value): void
{
$currencyCode = $this->currency->code ?? config('app.currency');
if ($value instanceof Money) {
$this->attributes[$attribute] = $value->getMinorAmount()->toScale(0, RoundingMode::UP)->toInt();
} else {
$this->attributes[$attribute] = (int) Money::of($value, $currencyCode, null, RoundingMode::UP)
->getMinorAmount()
->toScale(0, RoundingMode::UP)
->toInt();
}
}
}