Currency fields

This field will allow us to format the output of a value, base on the selected currency.

This field requires the installation of the extension for Php: INTL.

Php-intl install

To install the Php internationalization extension, we will do the following:

a) Local installation on Mac:

brew install intltool

b) In Ubuntu:

sudo apt-get install php-intl

And once installed, restart Apache, Ngix,...

Utilization

The field Currency, will allow us to manage the different currencies and their format. This field is based on the library:

Its basic operation would be:

use Daguilarm\Belich\Fields\Types\Currency;

/**
 * Get the fields displayed by the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return Illuminate\Support\Collection
 */
public function fields(Request $request): array
{
    return [
        Currency::make('Currency', 'test_decimal')
            ->currency('DKK'),
        Currency::make('Currency euro', 'test_decimal')
            ->setLocale('es_ES')
            ->euro(),
        Currency::make('Currency dollar', 'test_decimal')
            ->setLocale('en_US')
            ->dollar(),
    ];
}

The ISO codes of the currencies can be found at:

Supported methods

By default, the system will use the user's browser to determine the language setting, but this settings, can be changed with the method setlocale():

Currency::make('Currency euro', 'test_decimal')
    ->setLocale('es_ES'),

And using the country language code (More information).

We can also define the default currency, with the method currency() and the ISO codes for the currencies:

Currency::make('Currency euro', 'test_decimal')
    ->currency('EUR'),

To facilitate the work, we have created some helpers, for the most used currencies, which directly configure the currency, without using the currency () method:

  • dollar(): Set the currency in US dollars.
  • euro(): Set the currency in euros.
  • pound(): set the currency in pounds sterling.
  • yen(): set the currency in Japanese yen.
  • yuan(): set the currency in Chinese yuan.