Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bytefury's avatar

Best way to Save, Retrieve and Display money values in Laravel

I am currently saving all my money values as integer in the database. but i would love to see some laravel specific code to deal with money. if you have worked with money previously then please share some of your tips & tricks.

For example, I have a product which has price and shipping which should be stored in database as integers. Currently i am using Getters and setters for these both on my model to save and retrieve the values in proper readable format.

Any help would be appreciated.

0 likes
10 replies
noeldiaz's avatar

I started using getters and setters, but got into the problem that at some specific situations I did not want the value to be modified for formatting (like when I wanted to do some addition and calculations on the raw values).

What I ended up doing is taking my db values (stored as decimals) and passing them to a helper function I wrote to format it. I have a helpers file loading with composer with various little functions like this one. So I just end up passing the value to it and it comes back formatted correctly using the money_format php function.

So far that is what has worked best for me, but always on lookout of better alternatives if you find one.

brandonferens's avatar

You could cast the fields to a specific data type (assuming you are using Laravel 5+). http://laravel.com/docs/5.1/eloquent-mutators#attribute-casting. I would also recommend only attaching the dollar sign to a value when you are showing it to the user. Something like:

<span class="price">${{ $data->price }}</span>

This will help to keep the price value as a value, rather than a string that needs to be manipulated.

The other option though, that @noeldiaz mentioned as a helper might look like this:

<span class="price">{{ formatPrice($data->price) }}</span>

Then in a helper function, perhaps at app/Library/Helpers/helper.php, you could write:

if (!function_exists('formatPrice')) {

    /**
     * Format integer to a price
     *
     * @param integer $price
     *
     * @return string
     */
    function formatPrice($price)
    {
        // Do your necessary logic

        return $price;
    }
}

You could add a parameter that defaults to the dollar sign, but change it to something else if needed. Same with the decimal point as some currencies use commas.

Hope that helps you!

1 like
noeldiaz's avatar

The helper function is what I use. Php's money_format function is pretty handy to set the currency symbol and format:

http://php.net/money_format

That way I can format it for display when I need to, and keep as raw value for tables, calculations, etc.

JarekTkaczyk's avatar

@bytefury There's no laravel way nor should you look for one ;)

Anyway, use money_format, like @noeldiaz mentioned, and definitely don't use mutators/accessors for presentation purposes. Instead use presenters that will be responsible for showing prices accordingly, but on the model keep raw values.

The only exception to that could be use-case of a small API service that serves eloquent models directly - then I suggest using both raw value and formatted one (formatting done by the accessor in this case and property $appended to the JSON).

bytefury's avatar

Thanks a lot for the tips guys.

i am already using money_format for formatting the price using a helper function for presentation but i was also using accessors for dividing the values by 100 as i am saving them as integers. I think its better to use a helper function for converting the cents to Dollars instead of using accessors.

Rustproof's avatar

To get the integer value from your database ${{{ number_format((float) $item->price, 2) }}}

To get the percentage difference between a retail item and the new price.

{{number_format((float)(1-$item->retail_price/$item->price)100, 0)-1}}%

martinbean's avatar

Monetary values should be stored as an integer of the lowest denominator in the database. So for USD you should store the value in cents; for GBP the price in pence etc.

For formatting prices in your view, you can use a view presenter:

<?php

namespace App\Http\View\Presenters;

use App\Foo;
use Laravel\Cashier\Cashier;

class FooPresenter extends Presenter
{
    protected $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }

    public function priceFormatted()
    {
        return Cashier::formatAmount($this->foo->price);
    }
}
2 likes

Please or to participate in this conversation.