All Projects → cknow → Laravel Money

cknow / Laravel Money

Licence: mit
Laravel Money.

Projects that are alternatives of or similar to Laravel Money

Laravel Cconverter
A simple currency converter plugin for Laravel 5. Currency providers: The European Central Bank, OpenExchange, CurrencyLayer and fixer.io
Stars: ✭ 39 (-91.81%)
Mutual labels:  currency, laravel
Laravel Money
Currency formatting and conversion package for Laravel
Stars: ✭ 261 (-45.17%)
Mutual labels:  currency, laravel
Laravel Swap
💵 Currency exchange rates for Laravel and Lumen
Stars: ✭ 296 (-37.82%)
Mutual labels:  currency, laravel
Laratables
Ajax support of DataTables (Laravel 5.5+) Demo @
Stars: ✭ 451 (-5.25%)
Mutual labels:  laravel
Foroneadministrator
基于Laravel5.1封装的自带多级权限管理的后台管理系统,支持手机和PC端访问
Stars: ✭ 455 (-4.41%)
Mutual labels:  laravel
Laravel Medialibrary
Associate files with Eloquent models
Stars: ✭ 4,743 (+896.43%)
Mutual labels:  laravel
Laravel Backup
A package to backup your Laravel app
Stars: ✭ 4,752 (+898.32%)
Mutual labels:  laravel
Laravel Acquaintances
This package gives Eloquent models the ability to manage friendships (with groups), followships along with Likes, favorites..etc .
Stars: ✭ 449 (-5.67%)
Mutual labels:  laravel
Laravel Vue Boilerplate
🐘 A Laravel 6 SPA boilerplate with a users CRUD using Vue.js 2.6, GraphQL, Bootstrap 4, TypeScript, Sass, and Pug.
Stars: ✭ 472 (-0.84%)
Mutual labels:  laravel
Fullycms
Fully CMS - Multi Language Content Management System - Laravel
Stars: ✭ 465 (-2.31%)
Mutual labels:  laravel
Awesome Tall Stack
A curated list of awesome things related to the TALL stack.
Stars: ✭ 465 (-2.31%)
Mutual labels:  laravel
Framework
The truly Laravel E-commerce Framework
Stars: ✭ 456 (-4.2%)
Mutual labels:  laravel
Rest Api With Lumen
Rest API boilerplate for Lumen micro-framework.
Stars: ✭ 464 (-2.52%)
Mutual labels:  laravel
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (-5.25%)
Mutual labels:  laravel
Laravel React
Laravel 8 and React 17 boilerplate
Stars: ✭ 472 (-0.84%)
Mutual labels:  laravel
Telegram
✈️ Telegram Notifications Channel for Laravel
Stars: ✭ 450 (-5.46%)
Mutual labels:  laravel
Laracart
Laravel Shopping Cart Package
Stars: ✭ 470 (-1.26%)
Mutual labels:  laravel
Livewire Datatables
Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
Stars: ✭ 458 (-3.78%)
Mutual labels:  laravel
Laravel Boleto
Pacote para gerar boletos e remessas
Stars: ✭ 459 (-3.57%)
Mutual labels:  laravel
Aimeos Laravel
Laravel ecommerce package for professional, ultra fast online shops, complex B2B applications and #gigacommerce
Stars: ✭ 5,204 (+993.28%)
Mutual labels:  laravel

Laravel Money

Latest Stable Version Total Downloads tests StyleCI codecov License

Note: This project abstracts MoneyPHP

Installation

Run the following command from you terminal:

composer require cknow/laravel-money

or add this to require section in your composer.json file:

"cknow/laravel-money": "~6.0"

then run composer update

Usage

use Cknow\Money\Money;

echo Money::USD(500); // $5.00

Configuration

The defaults are set in config/money.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

php artisan vendor:publish --provider="Cknow\Money\MoneyServiceProvider"

This is the contents of the published file:

return [
    /*
     |--------------------------------------------------------------------------
     | Laravel money
     |--------------------------------------------------------------------------
     */
    'locale' => config('app.locale', 'en_US'),
    'defaultCurrency' => config('app.currency', 'USD'),
    'currencies' => [
        'iso' => ['RUB', 'USD', 'EUR'],  // 'all' to choose all ISOCurrencies
        'bitcoin' => ['XBT'], // 'all' to choose all BitcoinCurrencies
        'custom' => [
            'MY1' => 2,
            'MY2' => 3
        ]
    ]
];

Advanced Usage

See MoneyPHP for more information

use Cknow\Money\Money;

Money::USD(500)->add(Money::USD(500)); // $10.00
Money::USD(500)->add(Money::USD(500), Money::USD(500)); // $15.00
Money::USD(500)->subtract(Money::USD(400)); // $1.00
Money::USD(500)->subtract(Money::USD(200), Money::USD(100)); // $2.00
Money::USD(500)->multiply(2); // $10.00
Money::USD(1000)->divide(2); // $5.00
Money::USD(830)->mod(Money::USD(300)); // $2.30 -> Money::USD(230)
Money::USD(-500)->absolute(); // $5.00
Money::USD(500)->negative(); // $-5.00
Money::USD(30)->ratioOf(Money::USD(2)); // 15
Money::USD(500)->isSameCurrency(Money::USD(100)); // true
Money::USD(500)->equals(Money::USD(500)); // true
Money::USD(500)->greaterThan(Money::USD(100)); // true
Money::USD(500)->greaterThanOrEqual(Money::USD(500)); // true
Money::USD(500)->lessThan(Money::USD(1000)); // true
Money::USD(500)->lessThanOrEqual(Money::USD(500)); // true
Money::USD(500)->isZero(); // false
Money::USD(500)->isPositive(); // true
Money::USD(500)->isNegative(); // false
Money::USD(500)->getMoney(); // Instance of \Money\Money

// Aggregation
Money::min(Money::USD(100), Money::USD(200), Money::USD(300)); // Money::USD(100)
Money::max(Money::USD(100), Money::USD(200), Money::USD(300)); // Money::USD(300)
Money::avg(Money::USD(100), Money::USD(200), Money::USD(300)); // Money::USD(200)
Money::sum(Money::USD(100), Money::USD(200), Money::USD(300)); // Money::USD(600)

// Formatters
Money::USD(500)->format(); // $5.00
Money::USD(199)->format(null, null, \NumberFormatter::DECIMAL); // 1,99
Money::XBT(41000000)->formatByBitcoin(); // \xC9\x830.41
Money::USD(500)->formatByDecimal(); // 5.00
Money::USD(500)->formatByIntl(); // $5.00
Money::USD(199)->formatByIntl(null, null, \NumberFormatter::DECIMAL); // 1,99
Money::USD(500)->formatByIntlLocalizedDecimal(); // $5.00
Money::USD(199)->formatByIntlLocalizedDecimal(null, null, \NumberFormatter::DECIMAL) // 1.99

// Parsers
Money::parse('$1.00'); // Money::USD(100)
Money::parseByBitcoin("\xC9\x830.41"); // Money::XBT(41000000)
Money::parseByDecimal('1.00', 'USD'); // Money::USD(100)
Money::parseByIntl('$1.00'); // Money::USD(100)
Money::parseByIntlLocalizedDecimal('1.00', 'USD'); // Money::USD(100)

Create your formatter

class MyFormatter implements \Money\MoneyFormatter
{
    public function format(\Money\Money $money)
    {
        return 'My Formatter';
    }
}

Money::USD(500)->formatByFormatter(new MyFormatter()); // My Formatter

Casts

At this stage the cast can be defined in the following ways:

protected $casts = [
    // cast money using the currency defined in the package config
    'money' => MoneyCast::class,
    // cast money using the defined currency
    'money' => MoneyCast::class . ':AUD',
    // cast money using the currency defined in the model attribute 'currency'
    'money' => MoneyCast::class . ':currency',
];

In the example above, if the model attribute currency is null, the currency defined in the package configuration is used instead.

Setting money can be done in several ways:

$model->money = 10; // 10.00 USD or any other currency defined
$model->money = 10.23; // 10.23 USD or any other currency defined
$model->money = 'A$10'; // 10.00 AUD
$model->money = '1,000.23'; // 1000.23 USD or any other currency defined
$model->money = '10'; // 0.10 USD or any other currency defined
$model->money = Money::EUR(10); // 10 EUR

When we pass the model attribute holding the currency, such attribute is updated as well when setting money:

$model->currency; // null
$model->money = '€13';
$model->currency; // 'EUR'
$model->money->getAmount(); // '1300'

Helpers

currency('USD');
money(500); // To use default currency present in `config/money.php`
money(500, 'USD');

// Aggregation
money_min(money(100, 'USD'), money(200, 'USD'), money(300, 'USD')); // Money::USD(100)
money_max(money(100, 'USD'), money(200, 'USD'), money(300, 'USD')); // Money::USD(300)
money_avg(money(100, 'USD'), money(200, 'USD'), money(300, 'USD')); // Money::USD(200)
money_sum(money(100, 'USD'), money(200, 'USD'), money(300, 'USD')); // Money::USD(600)

// Parsers
money_parse('$5.00'); // Money::USD(500)
money_parse_by_bitcoin("\xC9\x830.41"); // Money::XBT(41000000)
money_parse_by_decimal('1.00', 'USD'); // Money::USD(100)
money_parse_by_intl('$1.00'); // Money::USD(100)
money_parse_by_intl_localized_decimal('1.00', 'USD'); // Money::USD(100)

Blade Extensions

@currency('USD')
@money(500) // To use default currency present in `config/money.php`
@money(500, 'USD')

// Aggregation
@money_min(@money(100, 'USD'), @money(200, 'USD'), @money(300, 'USD')) // Money::USD(100)
@money_max(@money(100, 'USD'), @money(200, 'USD'), @money(300, 'USD')) // Money::USD(300)
@money_avg(@money(100, 'USD'), @money(200, 'USD'), @money(300, 'USD')) // Money::USD(200)
@money_sum(@money(100, 'USD'), @money(200, 'USD'), @money(300, 'USD')) // Money::USD(600)

// Parsers
@money_parse('$5.00') // Money::USD(500)
@money_parse_by_bitcoin("\xC9\x830.41") // Money::XBT(41000000)
@money_parse_by_decimal('1.00', 'USD') // Money::USD(100)
@money_parse_by_intl('$1.00') // Money::USD(100)
@money_parse_by_intl_localized_decimal('1.00', 'USD') // Money::USD(100)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].