GithubHelp home page GithubHelp logo

avrahamappel / monetary-attributes Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 14 KB

Use dollar-based values in your Eloquent models and store them as integers.

License: MIT License

PHP 100.00%
laravel eloquent float decimals cents dollars

monetary-attributes's Introduction

Note: This repository is abandoned in favor of Eloquent Custom Casts, which were introduced in Laravel 7.

You can replicate the functionality of this package using Eloquent Custom Casts with the following Cast class:

<?php

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);
    }
}

Monetary Attributes

Monetary Attributes is a package for Laravel / Eloquent when you need to store monetary units, and don't want to deal with all the complexities that come when storing in float / double formats.

With this package you would store the money field in cents (no decimal point), and the package will convert it to dollars when retrieving and back to cents on storage.

Installation

Install the package via composer:

composer require appel/monetary-attributes

Usage

When creating a Eloquent model, instead of extending the standard Laravel model class, extend from the model class provided by this package:

<?php

namespace App;

use Appel\MonetaryAttributes\Model;

class Product extends Model
{
    //
}

Inside your Eloquent model, define the model's monetary attributes in the $moneyAttributes property.

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

To-Do

  • Switch to trait instead of using inheritance

monetary-attributes's People

Contributors

avrahamappel avatar bomshteyn avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.