Price - show as money but store in cents

I have a column "price" datatype INT I want to store the price in cents My form looks like, I'm implementing the money() method:
Forms\Components\TextInput::make('price')
->mask(fn (Forms\Components\TextInput\Mask $mask) => $mask->money(prefix: '£', thousandsSeparator: ',', decimalPlaces: 2, isSigned: false))
->required(),
Forms\Components\TextInput::make('price')
->mask(fn (Forms\Components\TextInput\Mask $mask) => $mask->money(prefix: '£', thousandsSeparator: ',', decimalPlaces: 2, isSigned: false))
->required(),
My table
Tables\Columns\TextColumn::make('price')
->money('gbp'),
Tables\Columns\TextColumn::make('price')
->money('gbp'),
When saving eg: £16,584.60 On the DB gets stored 16585 instead of 1658460 How can I have my form show £16,584.60 but when saving store in cents? Thanks.
4 Replies
Pablo Torres
Pablo TorresOP17mo ago
I have added this mutator to my model
public function setPriceAttribute($value)
{
$this->attributes['price'] = $value * 100;
}
public function setPriceAttribute($value)
{
$this->attributes['price'] = $value * 100;
}
Seems to be working but, is it the FilamentPHP way? Thanks.
benzo
benzo17mo ago
mutator is fine but this is more filament way
Pablo Torres
Pablo TorresOP17mo ago
Thanks, it works... I have updated adding to my Pages / Edit
protected function mutateFormDataBeforeSave(array $data): array
{
$data['price'] = $data['price'] * 100;

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['price'] = $data['price'] * 100;

return $data;
}
Thank you
Want results from more Discord servers?
Add your server