Skip to main content

Overview

Laravel Brick Money provides global helper functions that can be used anywhere in your application - controllers, models, views, service classes, and more. These helpers provide a convenient API for creating and formatting monetary values and currency objects.

money() Helper

The money() helper function creates a Money instance with automatic formatting based on the currency.

Signature

function money(
    BigNumber|float|int|string $amount,
    ?string $currency = null,
    ?bool $minor = null,
    ?Context $context = null,
    RoundingMode $roundingMode = RoundingMode::UNNECESSARY
): Money

Parameters

amount
BigNumber|float|int|string
required
The monetary amount. Can be a numeric string, integer, float, or Brick\Math\BigNumber instance.Examples: 100, "99.99", 99.99, BigDecimal::of('99.99')
currency
string
default:"config('brick-money.currency')"
The ISO 4217 currency code (e.g., “USD”, “EUR”, “GBP”, “JPY”).If not provided, uses the default currency from your config/brick-money.php configuration file.
minor
bool
default:"config('brick-money.minor')"
Whether the amount is in minor units (e.g., cents for USD).
  • When true: money(100) represents $1.00 (100 cents)
  • When false: money(100) represents $100.00
If not provided, uses the default from your config/brick-money.php configuration file.
context
Context
default:"null"
A Brick\Money\Context instance for custom precision and scale handling.Advanced usage only. Most applications won’t need to provide this parameter.
roundingMode
RoundingMode
default:"RoundingMode::UNNECESSARY"
The rounding mode from Brick\Math\RoundingMode to use when necessary.Options: UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_CEILING, HALF_FLOOR, HALF_EVEN, UNNECESSARY

Basic Usage

money(100)
// Output: $100.00
// Uses default currency from config

Examples

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CheckoutController extends Controller
{
    public function calculateTotal(Request $request)
    {
        $subtotal = money($request->input('subtotal'), 'USD');
        $shipping = money(995, 'USD', minor: true); // $9.95
        $tax = $subtotal->multipliedBy(0.08);
        
        $total = $subtotal
            ->plus($shipping)
            ->plus($tax);
        
        return response()->json([
            'subtotal' => $subtotal,
            'shipping' => $shipping,
            'tax' => $tax,
            'total' => $total,
        ]);
    }
}

Working with Money Objects

Once you create a Money instance with the helper, you can perform various operations:
$price = money(100, 'USD');

// Arithmetic operations
$price->plus(money(50, 'USD'));           // $150.00
$price->minus(money(25, 'USD'));          // $75.00
$price->multipliedBy(2);                   // $200.00
$price->dividedBy(4);                      // $25.00

// Comparisons
$price->isEqualTo(money(100, 'USD'));     // true
$price->isGreaterThan(money(50, 'USD'));  // true
$price->isLessThan(money(200, 'USD'));    // true

// Checks
$price->isPositive();                      // true
$price->isNegative();                      // false
$price->isZero();                          // false

// Formatting
$price->format();                          // "$100.00"
(string) $price;                           // "$100.00"
echo $price;                               // $100.00

// Access properties
$price->getAmount();                       // BigDecimal of 100
$price->getMinorAmount();                  // BigDecimal of 10000 (cents)
$price->getCurrency()->getCode();          // "USD"

currency() Helper

The currency() helper function creates a Currency instance for working with currency information.

Signature

function currency(?string $currency = null): Currency

Parameters

currency
string
default:"config('brick-money.currency')"
The ISO 4217 currency code (e.g., “USD”, “EUR”, “GBP”).If not provided, uses the default currency from your config/brick-money.php configuration file.

Basic Usage

currency()
// Uses config('brick-money.currency')

Examples

$usd = currency('USD');

$usd->getCode();                // "USD"
$usd->getName();                // "US Dollar"
$usd->getNumericCode();         // 840
$usd->getSymbol();              // "$"
$usd->isSymbolFirst();          // true
$usd->isSymbolSpaced();         // false
$usd->getDecimalPlaces();       // 2
$usd->getDecimalSeparator();    // "."
$usd->getThousandPlaces();      // 3
$usd->getThousandSeparator();   // ","

Implementation Details

Both helper functions are defined in src/helpers.php and automatically loaded by Composer.

money() Implementation

function money(
    BigNumber|float|int|string $amount,
    ?string $currency = null,
    ?bool $minor = null,
    ?Context $context = null,
    RoundingMode $roundingMode = RoundingMode::UNNECESSARY
): Money {
    if ($currency === null) {
        $currency = config('brick-money.currency');
    }

    if ($minor === null) {
        $minor = config('brick-money.minor');
    }

    return $minor
        ? Money::ofMinor($amount, $currency, $context, $roundingMode)
        : Money::of($amount, $currency, $context, $roundingMode);
}

currency() Implementation

function currency(?string $currency = null): Currency
{
    if ($currency === null) {
        $currency = config('brick-money.currency');
    }

    return Currency::of($currency);
}
Both helpers respect the default configuration values from config/brick-money.php, making it easy to set application-wide defaults while allowing per-call overrides.

Configuration Defaults

Set defaults in your config/brick-money.php file:
return [
    // Default currency for money() and currency() helpers
    'currency' => env('BRICK_MONEY_CURRENCY', 'USD'),
    
    // Whether amounts are in minor units by default
    'minor' => env('BRICK_MONEY_MINOR', false),
    
    // ... other configuration
];
With these defaults:
// Uses USD from config
money(100);  // $100.00

// Override with EUR
money(100, 'EUR');  // €100,00

// If minor = true in config:
money(100);  // $1.00 (100 cents)

// Override minor setting:
money(100, minor: false);  // $100.00
  • Blade Components - Use <x-money /> and <x-currency /> in templates
  • Blade Directives - Use @money and @currency directives in templates
  • Configuration - Configure default currency and behavior
  • HTTP - Use request()->money() and request()->currency() macros

Build docs developers (and LLMs) love