PHP code example of leancaptain / lara-money

1. Go to this page and download the library: Download leancaptain/lara-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/ */

    

leancaptain / lara-money example snippets


use LeanCaptain\Money\Currency;
use LeanCaptain\Money\RoundingMode;

return [
    'currency' => Currency::USD,
    'rounding_mode' => RoundingMode::REJECT,
];

use LeanCaptain\Money\MoneyFactory;

$factory = app(MoneyFactory::class);

$money = $factory->of('125.50');

use LeanCaptain\Money\MoneyFactory;

final class InvoiceService
{
    public function __construct(
        private MoneyFactory $money,
    ) {}
}

use LeanCaptain\LaraMoney\Casts\MoneyCast;

protected function casts(): array
{
    return [
        'amount' => MoneyCast::class,
    ];
}

$model->amount;

protected function casts(): array
{
    return [
        'amount' => MoneyCast::class,
    ];
}

protected function casts(): array
{
    return [
        'amount' => MoneyCast::class.':currency_code',
    ];
}

$model->amount = '125.50';

$model->amount = 125;

use LeanCaptain\Money\Currency;
use LeanCaptain\Money\Money;

$model->amount = Money::fromMinor(
    12550,
    Currency::BDT,
);

use LeanCaptain\Money\Currency;
use LeanCaptain\Money\Money;

$model->amount = Money::of(
    '125.50',
    Currency::BDT,
);

$model->amount = null;

$model->amount = 125.50; // rejected

$model->amount = '125.50';

$model->amount = Money::of('100', Currency::USD);

use LeanCaptain\Money\Contracts\CurrencyContract;

interface CurrencyContract
{
    public function code(): string;

    public function minorUnit(): int;
}

use LeanCaptain\LaraMoney\Contracts\CurrencyResolver;
use LeanCaptain\Money\Contracts\CurrencyContract;

final class AppCurrencyResolver implements CurrencyResolver
{
    public function resolve(string $code): CurrencyContract
    {
        return AppCurrency::from($code);
    }
}

use LeanCaptain\LaraMoney\Contracts\CurrencyResolver;

$this->app->singleton(
    CurrencyResolver::class,
    AppCurrencyResolver::class,
);

RoundingMode::REJECT
RoundingMode::HALF_UP
RoundingMode::DOWN

RoundingMode::REJECT
bash
php artisan vendor:publish --tag=lara-money-config
text
config/money.php
text
amount        = 12550
currency_code = EUR