Download the PHP package chrgriffin/eloquent-moneyphp without Composer
On this page you can find all versions of the php package chrgriffin/eloquent-moneyphp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chrgriffin/eloquent-moneyphp
More information about chrgriffin/eloquent-moneyphp
Files in chrgriffin/eloquent-moneyphp
Package eloquent-moneyphp
Short Description Automatically cast Eloquent columns to MoneyPHP objects.
License MIT
Informations about the package eloquent-moneyphp
Eloquent-MoneyPHP
Automatically cast Eloquent columns to MoneyPHP objects.
Installation
Install Eloquent-MoneyPHP with composer:
Requirements
- PHP >= 7.1.3
- Laravel >= 5.6
This package does make one key assumption: that you are storing money in your database as integers, not floating point values. For example, eight dollars would be stored as 800
, instead of 8.00
. To find out why you should store currency and other floating point values this way, read about the classic problem here.
Usage
Usage is extremely simple. Eloquent-MoneyPHP provides a trait that can be used on any Eloquent model in conjunction with an array of column names:
In the above setup, accessing the total_usd
or total_eur
attribute will automatically convert the attribute to a MoneyPHP object:
Eloquent-MoneyPHP also supports storing a money amount as a json string in a text column.
Then configure your model appropriately:
Under the Hood
Eloquent-MoneyPHP makes use of the Laravel magic methods getAttribute()
and setAttribute()
in conjunction with the configured array of column names to determine if it should cast a given attribute to a MoneyPHP object.
This could obviously prove problematic if you are already implementing getAttribute()
or setAttribute()
yourself. Luckily, you can include the package behaviour in the methods yourself, if you need to: