Currency Fields with Casts from Cents

Filament 4
Also available in Filament 3 version

A common Laravel and Filament requirement: Currency fields with relationships, select fields, dynamic prefixes, integer database storage with casts, and proper table formatting.

01J20YZR4SV57FCTBG0RSWXH0R

Get the Source Code:

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 price as an integer:

app/Casts/MoneyCast.php:

class MoneyCast implements CastsAttributes
{
public function get(Model $model, string $key, mixed $value, array $attributes): float
{
return round((float)$value / 100, precision: 2);
}
 
public function set(Model $model, string $key, mixed $value, array $attributes): float
{
return round((float)$value * 100);
}
}

Next, we had to implement it in our SubscriptionPlan Model:

The FULL tutorial is available after the purchase: in the Readme file of the official repository you would get invited to.
Get the Source Code: All 154 Premium Examples for $99