helvetitec/laramoney
laramoney
MoneyPHP implementation for Laravel
Installation
Install from composer:
composer require helvetitec/laramoney
Usage
- Use LaraMoneyCast to cast values to a json field like ['amount' => 100, 'currency' => 'brl']
- Use LaraMoneySimpleCast to cast values directly to an integer field with an optional 'currency' field inside the model.
- Use Money facade for anything else
LaraMoneyCast
It's easy to store and receive data from the database with the LaraMoneyCast. The value can either be NULL, an array of ['amount', 'currency'], a numeric value or a \Money\Money object.
The database field needs to be a JSON (compatible) field.
Add to the Models cast:
protected $casts = [
'price' => LaraMoneyCast::class
];
LaraMoneySimpleCast
The simple cast will store the value as a string, not as a JSON object for easier sorting etc. The value can either be NULL, an array of ['amount', 'currency'], a numeric value or a \Money\Money object.
The database field for price and currency needs to be a string.
Add to the Models cast:
protected $fillable = [
'price',
'currency' //This is the default field, but can be changed inside the config field 'model_currency_field'.
];
protected $casts = [
'price' => LaraMoneySimpleCast::class, //Will automatically set the 'currency' field as well
];
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | License |
|---|---|---|
| v1.5.9 | ||
| v1.5.8 | ||
| v1.5.7 | ||
| v1.5.6 | ||
| v1.5.5 | ||
| v1.5.4 | ||
| v1.5.3 | ||
| v1.5.2 | ||
| v1.5.1 | ||
| v1.5.0 | ||
| v1.4.1 | ||
| v1.4.0 | ||
| v1.3.2 | ||
| v1.3.1 | ||
| v1.3.0 | ||
| v1.2.1 | ||
| v1.2.0 | ||
| v1.1.1 | ||
| v1.1 | ||
| v1.0.12 | ||
| v1.0.11 | ||
| v1.0.10 | ||
| v1.0.9 | ||
| v1.0.8 | ||
| v1.0.7 | ||
| v1.0.6 | ||
| v1.0.5 | ||
| v1.0.4 | ||
| v1.0.3 | ||
| v1.0.2 |
Showing the latest 30 of 32. See every release on Packagist