Skip to content

Installation ​

bash
composer require laranex/laravel-money

The service provider, the LaravelMoney facade and the money() helper are registered automatically.

Laravel Money needs ext-bcmath (required by moneyphp/money). Install ext-intl for locale-aware formatting such as $1,234.50; without it, money formats as USD 1234.50.

Default Currency ​

Set the default currency in your .env. It must be an ISO 4217 code or a custom currency, and defaults to USD:

ini
MONEY_CURRENCY=USD

Configuration ​

Publishing the config file is optional; without it, the package reads MONEY_CURRENCY and uses the defaults below:

bash
php artisan vendor:publish --tag="laravel-money-config"

The laravel-money tag publishes the same file, config/money.php:

php
return [
    'default_currency' => env('MONEY_CURRENCY', 'USD'),

    'rounding' => 'half_up',

    'currencies' => [
        // 'PTS' => 0,
    ],

    'locale' => null,

    'serialization' => [
        'amount' => 'minor',
        'include_decimal' => true,
        'include_formatted' => true,
    ],
];
OptionDefaultDescription
default_currencyMONEY_CURRENCY, else USDUsed whenever no currency is given, and by casts without a currency. An ISO 4217 code or a custom currency
roundinghalf_upDefault rounding for multiplication, division, percentages, averages and roundTo(). See Rounding
currenciesnoneCustom currencies as code => decimal places, e.g. 'PTS' => 0. They take precedence over ISO 4217, so 'MMK' => 0 changes MMK's precision
localethe app localeLocale for format() and the formatted key, e.g. en, de_DE, my_MM
serializationminor + decimal + formattedThe serialized shape of Money

An unknown default currency or an invalid value throws an exception the first time it is used, so a typo shows up immediately.

Changing precision

Integer columns store minor units, so a currency's precision decides how they are read. Don't change the precision of a currency (in currencies) once amounts are stored in it.

What's Included ​

ClassContents
Laranex\LaravelMoney\MoneyThe Money value object
Laranex\LaravelMoney\RoundingThe rounding modes
Laranex\LaravelMoney\Facades\LaravelMoneyThe facade: building money, the configuration and the currency registry
Laranex\LaravelMoney\Casts\AsMoneyThe Eloquent casts
Laranex\LaravelMoney\Exceptions\*The exceptions

Released under the MIT License, except where a package says otherwise.