PHP code example of appel / monetary-attributes

1. Go to this page and download the library: Download appel/monetary-attributes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

appel / monetary-attributes example snippets




namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class Currency implements CastsAttributes
{
    /**
     * Cast the given value into currency format.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array  $attributes
     * @return string
     */
    public function get($model, string $key, $value, array $attributes)
    {
        return number_format((int) $value / 100, 2, '.', '');
    }

    /**
     * Cast the given value back into an integer for storage.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array  $attributes
     * @return int
     */
    public function set($model, string $key, $value, array $attributes)
    {
        return (int) round((float) $value * 100);
    }
}




namespace App;

use Appel\MonetaryAttributes\Model;

class Product extends Model
{
    //
}

...
class Product extends Model
{
    protected $moneyAttributes = ['price','sale_price'];
}
...