PHP code example of alexbabintsev / laravel-nbu

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

    

alexbabintsev / laravel-nbu example snippets


use AlexBabintsev\Nbu\Facades\Nbu;

Nbu::rate('USD')->rate;   // 44.7006
Nbu::rate('USD')->name;   // "Долар США"
Nbu::rate('USD')->date;   // CarbonImmutable

Nbu::rate('XYZ');         // null - the NBU publishes no such currency
Nbu::rateOrFail('XYZ');   // NbuException

Nbu::rate('USD', '2026-03-01');        // 43.2081
Nbu::rate('EUR', now()->subMonth());   // a DateTime works too

Nbu::rates();               // ['USD' => Rate, 'EUR' => Rate, ...] - 45 currencies
Nbu::rates('2026-03-01');

Nbu::toUah(100, 'USD');         // 4470.06
Nbu::fromUah(4470.06, 'USD');   // 100.0

// Or from the rate object itself
Nbu::rate('USD')->toUah(100);

Nbu::forget('2026-03-01');   // forget one day
Nbu::forget();               // forget the fallback

$rate = Nbu::rate('USD');

if (Nbu::isStale()) {
    // Served from the fallback - the bank did not answer today
}

// config/nbu.php
'timeout' => 5,              // NBU_TIMEOUT
'cache' => [
    'store' => null,         // NBU_CACHE_STORE, null = default store
    'prefix' => 'nbu',       // NBU_CACHE_PREFIX
    'fallback_days' => 30,   // NBU_FALLBACK_DAYS
],

Http::fake(['bank.gov.ua/*' => Http::response([
    ['r030' => 840, 'txt' => 'Долар США', 'rate' => 44.6144, 'cc' => 'USD', 'exchangedate' => '21.08.2026'],
])]);

use AlexBabintsev\Nbu\Facades\Nbu;

Nbu::rate('USD')->rate;   // 44.7006
Nbu::rate('USD')->name;   // "Долар США"
Nbu::rate('USD')->date;   // CarbonImmutable

Nbu::rate('XYZ');         // null - НБУ не публікує таку валюту
Nbu::rateOrFail('XYZ');   // NbuException

Nbu::rate('USD', '2026-03-01');        // 43.2081
Nbu::rate('EUR', now()->subMonth());   // приймає і DateTime

Nbu::rates();               // ['USD' => Rate, 'EUR' => Rate, ...] - 45 валют
Nbu::rates('2026-03-01');

Nbu::toUah(100, 'USD');         // 4470.06
Nbu::fromUah(4470.06, 'USD');   // 100.0

// Або через сам об'єкт курсу
Nbu::rate('USD')->toUah(100);

Nbu::forget('2026-03-01');   // забути конкретний день
Nbu::forget();               // забути резервне значення

$rate = Nbu::rate('USD');

if (Nbu::isStale()) {
    // Курс із резерву - банк сьогодні не відповів
}

// config/nbu.php
'timeout' => 5,              // NBU_TIMEOUT
'cache' => [
    'store' => null,         // NBU_CACHE_STORE, null = типове сховище
    'prefix' => 'nbu',       // NBU_CACHE_PREFIX
    'fallback_days' => 30,   // NBU_FALLBACK_DAYS
],

Http::fake(['bank.gov.ua/*' => Http::response([
    ['r030' => 840, 'txt' => 'Долар США', 'rate' => 44.6144, 'cc' => 'USD', 'exchangedate' => '21.08.2026'],
])]);
bash
php artisan vendor:publish --tag=nbu-config
bash
php artisan vendor:publish --tag=nbu-config