kodeas / currency
A simple currency cast for Laravel
1.0.5
2023-01-27 18:44 UTC
Requires
- php: ^8.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-09 12:00:24 UTC
README
A simple currency cast for Laravel
This package is created to eliminate the ambiguity involved in storing and retrieving currencies. Currently, for simplicity the terminology surrounding the package is in US currency, however there is nothing stopping anyone from using this for any other 2 decimal currencies.
Installation
You can install the package via composer:
composer require kodeas/currency
Usage
MyModel extends Model { protected $casts = [ 'amount' => Kodeas\Currency\Casts\Currency::class ]; }
$currency = Kodeas\Currency\Currency::fromUsd(1); $model = MyModel::create([ 'amount' => $currency //100(cents) in database ]); $model->amount //Currency::class
Initialize
$currency = Kodeas\Currency\Currency::fromUsd(1); $currency = Kodeas\Currency\Currency::fromCents(100);
Methods
echo $currency; // "1.00" $currency->toUsd(); // "1" $currency->toCents(); // "100" $currency->toReadable(); // "1.00" $currency->toReadable('$'); // "$1.00" return response()->json(['currency' => $currency->toUsd()]); // {"currency": "1.00"}
Credits
License
The MIT License (MIT). Please see License File for more information.