Jump to content

How better to work with money on mysql / laravel site ?


Recommended Posts

Working with money in php 8 /laravel 9 site for money fields I use integer in mysql db and custom cast app/Casts/MoneyCast.php like :


 

<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class MoneyCast implements CastsAttributes
{

    public function get($model, string $key, $value, array $attributes)
    {
        return round(floatval($value) / 100, 2);
    }

    public function set($model, string $key, $value, array $attributes)
    {
        return floatval($value) * 100;
    }
}

 

It seems quite good for me, but I found some referance to libraries

https://github.com/brick/money
and
https://github.com/akaunting/laravel-money

and wonder if some of which library is preferable for working in laravel ?
Would it be better or safer ?

Thanks in advance!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.