Skip to main content
Laravel Brick Money provides powerful Model casts that seamlessly integrate with Eloquent, allowing you to work with Money and Currency objects while abstracting away the complexity of database storage.

Available Cast Types

The package provides three cast types for different use cases:

AsIntegerMoney

Store money in minor units (cents) - Recommended

AsDecimalMoney

Store money in major units (dollars)

AsCurrency

Cast currency codes to Currency objects

Storage Strategies

Each money cast supports two storage strategies: Store the amount and currency in separate database columns. This approach offers better queryability and indexing capabilities.
use Devhammed\LaravelBrickMoney\Casts\AsIntegerMoney;

protected function casts(): array
{
    return [
        'price' => AsIntegerMoney::of('currency'),
    ];
}
The first argument to of() specifies the currency column name. The amount is stored in the cast attribute itself (price in this example).

JSON Column

Store both amount and currency in a single JSON column. This is useful when you want to keep monetary values self-contained.
use Devhammed\LaravelBrickMoney\Casts\AsIntegerMoney;

protected function casts(): array
{
    return [
        'price' => AsIntegerMoney::class,
    ];
}

Quick Comparison

Cast TypeStorage FormatUse CasePrecision
AsIntegerMoneyMinor units (cents)General purpose, cryptocurrenciesExact
AsDecimalMoneyMajor units (dollars)Human-readable storageDecimal
AsCurrencyCurrency codeCurrency reference onlyN/A

Basic Usage Example

1

Define the cast in your model

use Devhammed\LaravelBrickMoney\Money;
use Devhammed\LaravelBrickMoney\Casts\AsIntegerMoney;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected function casts(): array
    {
        return [
            'price' => AsIntegerMoney::of('currency'),
        ];
    }
}
2

Create database migration

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->bigInteger('price');
    $table->string('currency', 3);
    $table->timestamps();
});
3

Work with Money objects

// Create a product
$product = Product::create([
    'price' => Money::of(99.99, 'USD'),
]);

// Retrieve and use
$product = Product::find(1);
echo $product->price->format(); // "$99.99"

// Perform calculations
$taxAmount = $product->price->multipliedBy(0.1);
$total = $product->price->plus($taxAmount);
echo $total->format(); // "$109.99"

Key Benefits

Work with strongly-typed Money objects throughout your application instead of raw numbers.
The casts handle all serialization and deserialization between your database and PHP objects.
Each Money object knows its currency, preventing accidental mixing of different currencies.
Avoid floating-point arithmetic issues with proper money handling using the Brick\Money library.

Choosing the Right Cast

Always use AsIntegerMoney unless you have a specific reason not to. Storing money as integers (minor units) prevents floating-point precision issues and is the industry standard.

Use AsIntegerMoney when:

  • Working with standard currencies (USD, EUR, GBP, etc.)
  • Handling cryptocurrencies with high precision
  • You need exact arithmetic without rounding errors
  • Building financial applications (recommended)

Use AsDecimalMoney when:

  • You need human-readable values in the database
  • Integrating with legacy systems that store decimals
  • Working with reporting tools that expect decimal values

Use AsCurrency when:

  • You only need to store and retrieve currency information
  • Working alongside money casts for the currency column
  • Building multi-currency product catalogs

Next Steps

AsIntegerMoney Cast

Learn about the recommended cast for storing money

AsDecimalMoney Cast

Explore decimal-based money storage

Build docs developers (and LLMs) love