https://github.com/grohiro/laravel-money
Custom cast library
https://github.com/grohiro/laravel-money
Last synced: 21 days ago
JSON representation
Custom cast library
- Host: GitHub
- URL: https://github.com/grohiro/laravel-money
- Owner: grohiro
- Created: 2022-04-21T09:02:09.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-18T04:04:24.000Z (almost 4 years ago)
- Last Synced: 2025-02-24T01:48:25.730Z (over 1 year ago)
- Language: PHP
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# laravel-money
Laravel custom cast library for [moneyphp/money](https://github.com/moneyphp/money).
## Installation
```bash
$ composer require grohiro/laravel-money
```
## Usage
```sql
alter table products add column price decimal(11, 0);
alter table products add column price_currency varchar(3);
```
```php
class Product extends Model
{
protected $casts = [
'price' => MoneyCast::class,
];
}
$product->price = new \Money\Money(1050, new \Money\Currency('USD'));
# price => 1050, price_currency => 'USD'
$product->price = \money('10.5', 'USD'); // see src/functions.php
# price => 1050, price_currency => 'USD'
$json = MoneyHelper::json($product->price);
print_r($json);
/*
'amount' => 1050,
'decimal' => '10.5',
'code' => 'USD',
'text' => '10.5USD',
*/
```
## Tests
```bash
$ composer test
```