Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/snoeren-development/laravel-currency-casting

Cast currency attributes stored as integer to floats automatically.
https://github.com/snoeren-development/laravel-currency-casting

cast composer currency laravel laravel-package php

Last synced: 11 days ago
JSON representation

Cast currency attributes stored as integer to floats automatically.

Awesome Lists containing this project

README

        

# Laravel Currency Casting
[![Latest version on Packagist](https://img.shields.io/packagist/v/snoeren-development/laravel-currency-casting.svg?style=flat-square)](https://packagist.org/packages/snoeren-development/laravel-currency-casting)
[![Software License](https://img.shields.io/github/license/snoeren-development/laravel-currency-casting?style=flat-square)](LICENSE)
[![Build status](https://img.shields.io/github/actions/workflow/status/snoeren-development/laravel-currency-casting/php.yml?style=flat-square)](https://github.com/snoeren-development/laravel-currency-casting/actions)
[![Downloads](https://img.shields.io/packagist/dt/snoeren-development/laravel-currency-casting?style=flat-square)](https://packagist.org/packages/snoeren-development/laravel-currency-casting)

This package adds a Laravel model cast. This way you can cast any attribute that stores a currency, with an integer value in the database, to a float automatically!

## Installation
You can install the package using Composer:
```bash
composer require snoeren-development/laravel-currency-casting
```

### Requirements
This package requires **at least** PHP 8.2 and Laravel 10.

### Usage
Store your currency as an integer value in the database. This is more accurate than storing it as a float.
Add the attributes you'd like to see cast to the `casts` array and assign the `Currency` class to it. If you need more than the default 2 digits currency usually has, you can append the number of digits you need after the currency class like in the example below. Just make sure your database column can handle the larger integer it produces.
```php
Currency::class,
'price_with_digits' => Currency::class . ':4',
];

//
}
```

## Testing
```bash
$ composer test
```

## Credits
- [Michael Snoeren](https://github.com/MSnoeren)
- [All Contributors](https://github.com/snoeren-development/laravel-currency-casting/graphs/contributors)

## License
The MIT license. See [LICENSE](LICENSE) for more information.