Download the PHP package makeabledk/laravel-currencies without Composer
On this page you can find all versions of the php package makeabledk/laravel-currencies. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-currencies
Laravel Currencies
This package provides a convenient and powerful way of interacting with currencies and amounts in Laravel.
Install
You can install this package via composer:
On Laravel versions < 5.5, you must include the service provider in you config/app.php
:
After installation you mus publish and run migrations to create the currencies
table
Setup
Recommended: Seed currencies
This package provides a eloquent 'Currency' model out of the box that fetches available currencies from your database.
Often you would want to seed these currencies from your code in order to normalize them across environments. This can easily be achieved with the Laravel Production Seeding package.
Create the following seeder and let it run on each deployment:
Now your database table should look something like this:
id | code | exchange_rate |
---|---|---|
1 | EUR | 100.00 |
2 | DKK | 750.00 |
Tip: Consider using [https://github.com/dwightwatson/rememberable]() to cache currencies and throttle database queries.
Tip: If you don't want to hardcode exchange rates, create a console-command that fetches and updates from an external service, and ommit the field from the seeder.
Register base currency (required)
The amount object requires a base currency that it uses to convert between currencies.
The exchange rates given for your currencies must all relate to the base currency.
Define it in your AppServiceProvider@boot
:
Register default currency (optional)
Additionally you have the option to define a default currency if this is not the same as your base-currency.
You may want to have a global currency such as USD or EUR for your base-currency to perform conversions, meanwhile your application defaults to display a local currency.
This can be achieved by defining a default-currency in your AppServiceProvider@boot
as well:
Now when instantiating an amount without an explicit Currency it will default to 'DKK':
Example usages
Quickly create an amount
Convert between currencies
Perform simple calculations - even between currencies!
Imagine you have a Product eloquent model with a @getPriceAttribute() accessor that returns an Amount object, you can even do this:
Use the fluent modifiers for easy manipulation
Easily export as an array, and re-instantiate if needed. Great for serving client API*.
*Note it implements illuminate/support Arrayable contract, so it automatically casts to an array for eloquent models.
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
You can run the tests with:
Contributing
We are happy to receive pull requests for additional functionality. Please see CONTRIBUTING for details.
Credits
- Rasmus Christoffer Nielsen
- All Contributors
License
Attribution-ShareAlike 4.0 International. Please see License File for more information.
All versions of laravel-currencies with dependencies
illuminate/container Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0