1. Go to this page and download the library: Download akaunting/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/ */
akaunting / laravel-money example snippets
use Akaunting\Money\Currency;
use Akaunting\Money\Money;
echo Money::USD(500); // '$5.00' unconverted
echo new Money(500, new Currency('USD')); // '$5.00' unconverted
echo Money::USD(500, true); // '$500.00' converted
echo new Money(500, new Currency('USD'), true); // '$500.00' converted
use Akaunting\Money\Currency;
use Akaunting\Money\Money;
Money::macro('zero', fn (?string $currency = null) => new Money(0, new Currency($currency ?? 'GBP')));
$money = Money::zero();
use Akaunting\Money\Currency;
use Akaunting\Money\Money;
class CustomMoney
{
public function absolute(): callable
{
return function(): Money {
/** @var Money $this */
return $this->isPositive() ? $this : $this->multiply(-1);
};
}
public function zero(): callable
{
return fn(?string $currency = null): Money => new Money(0, new Currency($currency ?? 'GBP'));
}
public function sum(): callable
{
return function (iterable $items, null|string|Currency $currency = null): Money {
$items = collect($items)->ensure(Money::class);
return $items->reduce(
fn(Money $sum, Money $item) => $sum->add($item),
new Money(0, $currency instanceof Currency ? $currency : (
$items->isEmpty() || is_string($currency) ? new Currency($currency ?? 'GBP') : $items->first()->getCurrency()
))
);
};
}
}
Money::mixin(new CustomMoney);
$money = Money::USD(1000)->multiply(-1);
$absolute = $money->absolute();
// Static methods via mixins are supported too:
$money = Money::zero();
bash
php artisan vendor:publish --tag=money
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.