F
Filament•2w ago
Nahian

Show Correct Currency Values in Edit Views with Laravel Money

Hello 👋, I’m integrating laravel-money with the following workflow: - During record creation, users can enter an amount in their preferred currency (for example, BDT or USD). - When saving, laravel-money should automatically convert the amount into its minor unit (such as cents). - When editing, the form should display the value in the original currency chosen by the user. - When viewing records in tables, the displayed amount should match what’s shown in the edit form. I’ve already configured the create and show functionality using Eloquent Model attribute casting:
'price' => MoneyIntegerCast::class . ':currency',
'price' => MoneyIntegerCast::class . ':currency',
The database table has two columns: price and currency. My question: How can I display the correct currency value in the edit form using laravel-money? https://github.com/cknow/laravel-money
GitHub
GitHub - cknow/laravel-money: Laravel Money.
Laravel Money. Contribute to cknow/laravel-money development by creating an account on GitHub.
1 Reply
Nahian
NahianOP•2w ago
I think I've found a possible answer. I just added these in the Edit record class (for my case, it's EditProduct.php)
protected function mutateFormDataBeforeFill(array $data): array
{
$data['price_minimum'] = $data['price_minimum']['amount'] / 100;
$data['price_maximum'] = $data['price_maximum']['amount'] / 100;
$data['bid_increment'] = $data['bid_increment']['amount'] / 100;

return $data;
}
protected function mutateFormDataBeforeFill(array $data): array
{
$data['price_minimum'] = $data['price_minimum']['amount'] / 100;
$data['price_maximum'] = $data['price_maximum']['amount'] / 100;
$data['bid_increment'] = $data['bid_increment']['amount'] / 100;

return $data;
}

Did you find this page helpful?