Currency Fields with Casts from Cents

Filament 3
Also available in Filament 4/5 version

Dealing with Money can be complicated. It's not only about float/integer in DB, but also about Currency, especially with multiple currencies. We will show you how to deal with them, in this example.

FilamentExamples ThumbBase (26)

Get the Source Code:

Only This Example

$9

One-time payment

Full source code for Currency Fields with Casts from Cents
Downloadable ZIP file with the source code
Lifetime access to this example
GitHub Sign in with GitHub to buy

Sign in first, then complete your $9 checkout.

Best value — all 172 examples

FilamentExamples Membership

$99 /year
or
$199 lifetime
Access to code of all 172 examples
Future new examples and updates included
FilaCheck Pro package licence included
MCP server included
View membership plans

30-day money-back guarantee

How it works

First, we have created a Currency Model with a name and symbol fields. Then, we have seeded some basic currencies:

database/seeders/DatabaseSeeder.php

use App\Models\Currency;
 
// ...
 
$currencies = [
[
'name' => 'USD',
'symbol' => '$',
],
[
'name' => 'EUR',
'symbol' => '€',
],
[
'name' => 'GBP',
'symbol' => '£',
],
];
 
foreach ($currencies as $currency) {
Currency::create($currency);
}
 
// ...

Next, we created a model that uses currency. In our case, this was SubscriptionPlan which has a price. To store the price, we have created a MoneyCast that stores the price as an integer in the database:

php artisan make:cast MoneyCast

In the MoneyCast, we have defined the set and get methods to store and retrieve the...

The FULL tutorial is available after the purchase: in the Readme file of the official repository you would get invited to.