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.
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
Simple Amount
With Currency
Minor Units
Multiple Parameters
money ( 100 )
// Output: $100.00
// Uses default currency from config
Examples
Controllers
Models
Service Classes
Blade Templates
API Resources
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 ,
]);
}
}
namespace App\Models ;
use Illuminate\Database\Eloquent\ Model ;
use Devhammed\LaravelBrickMoney\ Money ;
class Product extends Model
{
public function getPriceAttribute () : Money
{
// Convert cents stored in DB to Money object
return money ( $this -> price_cents , $this -> currency , minor : true );
}
public function getDiscountedPriceAttribute () : Money
{
return $this -> price -> multipliedBy ( 1 - ( $this -> discount_percent / 100 ));
}
public function getTaxAmountAttribute () : Money
{
return $this -> price -> multipliedBy ( $this -> tax_rate / 100 );
}
public function getTotalPriceAttribute () : Money
{
return $this -> price -> plus ( $this -> tax_amount );
}
}
namespace App\Services ;
use Devhammed\LaravelBrickMoney\ Money ;
class PaymentService
{
public function calculateProcessingFee ( Money $amount ) : Money
{
// 2.9% + $0.30
$percentageFee = $amount -> multipliedBy ( 0.029 );
$fixedFee = money ( 30 , $amount -> getCurrency () -> getCode (), minor : true );
return $percentageFee -> plus ( $fixedFee );
}
public function convertCurrency (
Money $amount ,
string $targetCurrency ,
float $exchangeRate
) : Money {
return $amount -> convertedTo ( $targetCurrency , $exchangeRate );
}
public function splitPayment ( Money $total , int $installments ) : array
{
return $total -> split ( $installments );
}
}
{{-- In controllers, pass to view --}}
public function show(Product $product)
{
return view('products.show', [
'product' => $product,
'price' => money($product->price_cents, $product->currency, minor: true),
]);
}
{{-- In blade templates --}}
< div class = "product-details" >
< h1 > {{ $product -> name }} </ h1 >
< p class = "price" > {{ money ( $product -> price_cents , $product -> currency , minor : true ) }} </ p >
@if ( $product -> on_sale )
< p class = "sale-price" >
{{ money ( $product -> sale_price_cents , $product -> currency , minor : true ) }}
</ p >
@endif
</ div >
namespace App\Http\Resources ;
use Illuminate\Http\Resources\Json\ JsonResource ;
class ProductResource extends JsonResource
{
public function toArray ( $request ) : array
{
return [
'id' => $this -> id ,
'name' => $this -> name ,
'price' => money (
$this -> price_cents ,
$this -> currency ,
minor : true
),
'discounted_price' => $this -> when (
$this -> discount_percent > 0 ,
fn () => money ( $this -> price_cents , $this -> currency , minor : true )
-> multipliedBy ( 1 - ( $this -> discount_percent / 100 ))
),
];
}
}
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
Default Currency
Specific Currency
Get Currency Info
currency ()
// Uses config('brick-money.currency')
Examples
Get Currency Details
Compare Currencies
Currency Selection
Format Display
$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 (); // ","
$usd = currency ( 'USD' );
$eur = currency ( 'EUR' );
$anotherUsd = currency ( 'USD' );
$usd -> is ( $eur ); // false
$usd -> is ( $anotherUsd ); // true
$usd -> is ( 'USD' ); // true
$usd -> is ( 'EUR' ); // false
public function getCurrencies () : array
{
$codes = [ 'USD' , 'EUR' , 'GBP' , 'JPY' , 'CAD' ];
return collect ( $codes ) -> map ( function ( $code ) {
$currency = currency ( $code );
return [
'code' => $currency -> getCode (),
'name' => $currency -> getName (),
'symbol' => $currency -> getSymbol (),
'decimal_places' => $currency -> getDecimalPlaces (),
];
}) -> all ();
}
public function formatPrice ( int $cents , string $currencyCode ) : string
{
$currency = currency ( $currencyCode );
$money = money ( $cents , $currencyCode , minor : true );
// Custom formatting based on currency properties
$symbol = $currency -> getSymbol ();
$amount = $money -> format ();
if ( $currency -> isSymbolFirst ()) {
$separator = $currency -> isSymbolSpaced () ? ' ' : '' ;
return $symbol . $separator . $amount ;
}
return $amount ;
}
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