PHP code example of makeabledk / laravel-currencies

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

    

makeabledk / laravel-currencies example snippets


'providers' => [
...
    /*
     * Package Service Providers...
     */
     
    \Makeable\LaravelCurrencies\CurrenciesServiceProvider::class,
]



class CurrencySeeder extends \Illuminate\Database\Seeder
{
    use \Makeable\ProductionSeeding\SyncStrategy;

    /**
     * @var array
     */
    protected $currencies = [
        [
            'code' => 'EUR',
            'exchange_rate' => 100
        ], [
            'code' => 'DKK',
            'exchange_rate' => 750
        ],
        // ... 
    ];

    /**
     * Seed the currencies
     */
    public function run()
    {
        $this->apply($this->currencies, \Makeable\LaravelCurrencies\Currency::class, 'code');
    }
}

public function boot() {
    $this->app->singleton(\Makeable\LaravelCurrencies\Contracts\BaseCurrency::class, function () {
        return \Makeable\LaravelCurrencies\Currency::fromCode('EUR');
    });
}

public function boot() {
    // Define base currency 
    // [...]

    // Define default currency
    $this->app->singleton(\Makeable\LaravelCurrencies\Contracts\BaseCurrency::class, function () {
        return \Makeable\LaravelCurrencies\Currency::fromCode('DKK');
    });
}

new Amount (100); // 100 DKK
Amount::zero(); // 0 DKK

new Amount(100); // EUR since that's our default
new Amount(100, Currency::fromCode('DKK')); 
new Amount(100, 'DKK'); // It automatically instantiates a currency instance given a currency-code

$eur = new Amount(100);
$dkk = $eur->convertTo('DKK'); // 750 DKK

$amount = new Amount(100, 'EUR');
$amount->subtract(new Amount(50)); // 50 eur
$amount->subtract(new Amount(375, 'DKK')); // 50 eur

$products = Product::all();
$productsTotalSum = Amount::sum($products, 'price'); 

$amount = new Amount(110);

// Ensure that the amount at least a certain amount
$amount->minimum(new Amount(80)); // 110 EUR
$amount->minimum(new Amount(120)); // 120 EUR

// Ensure that the amount is no bigger than a certain amount
$amount->maximum(new Amount(80)); // 80 EUR
$amount->maximum(new Amount(750, 'DKK'); // 100 EUR (eq. 750 DKK)

$exported = (new Amount(100))->toArray(); // ['amount' => 100, 'currency' => 'EUR', 'formatted' => 'EUR 100']
$imported = Amount::fromArray($exported);
bash
php artisan vendor:publish --provider="Makeable\LaravelCurrencies\CurrenciesServiceProvider"
php artisan migrate