1. Go to this page and download the library: Download cknow/laravel-money library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cknow / laravel-money example snippets
use Cknow\Money\Money;
echo Money::USD(500); // $5.00
echo Money::USD(500, true); // $500.00 force decimals
class MyFormatter implements \Money\MoneyFormatter
{
public function format(\Money\Money $money)
{
return 'My Formatter';
}
}
Money::USD(500)->formatByFormatter(new MyFormatter()); // My Formatter
Validator::make([
'currency1' => 'USD',
'currency2' => 'EUR',
'currency3' => new \Money\Currency('BRL'),
], [
'currency1' => new \Cknow\Money\Rules\Currency(),
'currency2' => new \Cknow\Money\Rules\Currency(),
'currency3' => 'currency',
]);
use Cknow\Money\Casts\MoneyDecimalCast;
use Cknow\Money\Casts\MoneyIntegerCast;
use Cknow\Money\Casts\MoneyStringCast;
protected $casts = [
// cast money as decimal using the currency defined in the package config
'money' => MoneyDecimalCast::class,
// cast money as integer using the defined currency
'money' => MoneyIntegerCast::class . ':AUD',
// cast money as string using the currency defined in the model attribute 'currency'
'money' => MoneyStringCast::class . ':currency',
// cast money as decimal using the defined currency and forcing decimals
'money' => MoneyDecimalCast::class . ':USD,true',
];
$model->money = 10; // 0.10 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); // 0.10 EUR