Install Laravel Money
mainInstall the package via Composer to add money handling capabilities to your Laravel application.
composer require cknow/laravel-moneyrepository·main·Indexed 21 days ago
https://github.com/cknow/laravel-moneyA wrapper around MoneyPHP for the Laravel framework. It provides seamless integration through Eloquent casts (MoneyDecimalCast, MoneyIntegerCast, MoneyStringCast), custom validation rules for currency and money, Blade directives for formatting and aggregation, and global helpers. The library supports money arithmetic, comparisons, and custom formatting via the MoneyFormatter interface.
Install the package via Composer to add money handling capabilities to your Laravel application.
composer require cknow/laravel-moneyIntegrate money handling directly into your Eloquent models using the provided casts. This allows you to store money as integers, decimals, or strings in the database while interacting with them as money objects or formatted values in your application.
use Cknow\Money\Casts\MoneyDecimalCast;
use Cknow\Money\Casts\MoneyIntegerCast;
use Cknow\Money\Casts\MoneyStringCast;
protected $casts = [
'money' => MoneyDecimalCast::class, // Uses default currency from config
'money' => MoneyIntegerCast::class . ':AUD', // Forces AUD
'money' => MoneyStringCast::class . ':currency', // Uses 'currency' attribute on model
];MoneyServiceProvider. This allows you to use specialized Blade directives to format money and currencies directly within your .blade.php templates.Publish the configuration file to config/money.php to customize locales, default currencies, and supported currency lists (ISO, Bitcoin, or custom).
php artisan vendor:publish --provider="Cknow\Money\MoneyServiceProvider"To customize the package behavior, you can publish the configuration file to your application's config directory using the config tag. This allows you to modify the default settings defined in the package.
php artisan vendor:publish --tag=configUse the Cknow\Money\Money class to create money instances using static methods named after currency codes. The amount passed is in the smallest unit (e.g., cents for USD).
use Cknow\
Money\Money;
echo Money::USD(500); // $5.00
echo Money::USD(500, true); // $500.00 force decimalsYou can create custom formatting logic by implementing the \Money\MoneyFormatter interface and passing it to the formatByFormatter() method.
class MyFormatter implements \Money\MoneyFormatter
{
public function format(\Money\Money $money): string
{
return 'My Formatter';
}
}
Money::USD(500)->formatByFormatter(new MyFormatter()); // My FormatterThe Money class provides methods for arithmetic operations (add, subtract, multiply, divide, mod), sign manipulation (absolute, negative), and comparisons (equals, greaterThan, lessThan, etc.). It also supports aggregation functions like min, max, avg, and sum.
use Cknow\Money\Money;
// Arithmetic
Money::USD(500)->add(Money::USD(500)); // $10.00
Money::USD(500)->subtract(Money::USD(400)); // $1.00
Money::USD(500)->multiply(2); // $10.00
Money::USD(1000)->divide(2); // $5.00
// Comparisons
Money::USD(500)->equals(Money::USD(500)); // true
Money::USD(500)->greaterThan(Money::USD(100)); // true
// Aggregation
Money::sum(Money::USD(100), Money::USD(200), Money::USD(300)); // Money::USD(600)The library provides several ways to format money as strings (using Intl, currency symbols, or decimals) and parse strings back into Money instances (via Intl, decimal, or Bitcoin formats).
use Cknow\Money\Money;
// Formatting
Money::USD(500)->format(); // $5.00
Money::USD(500)->formatByIntl(); // $5.00
// Parsing
Money::parse('$1.00'); // Money::USD(100)
Money::parseByDecimal('1.00', 'USD'); // Money::USD(100)ISOCurrencies class loads its currency definitions from a file specified in the Laravel configuration. You can customize the location of this file by setting the money.isoCurrenciesPath configuration key. This is useful if you need to provide a custom list of ISO currencies or override the default implementation.The package provides global helper functions for quick access to money operations without needing to call the Money class directly.
currency(); // Default currency from config
currency('USD');
money(500); // Default currency
money(500, 'USD');
money_parse('$5.00'); // Money::USD(500)
// Aggregation helpers
money_sum(money(100, 'USD'), money(200, 'USD'));Use the following validation rules in your Laravel controllers or FormRequests:
currency: Validates that a field is a valid currency code.money: Validates that a field is a valid money string. You can force a specific currency using money:CURRENCY or a locale using money:CURRENCY,LOCALE.// Currency validation
Validator::make(['c' => 'USD'], ['c' => new \Cknow\Money\Rules\Currency()]);
// Money validation
Validator::make(['m' => '$10.00'], ['m' => 'money:EUR,pt_BR']);