How to store float values efficiently in Laravel Eloquent ?

· · 79 words · 1 minute read

If you want to store price in dollars such as 4.99, just store it as integer not float in database (499). And in your model add this function.

public function getPriceAttribute(){
    return floatval(number_format($this->attributes["price"] / 100, 2));
}

When you get the price, it is 4.99 , but it is stored as 499. This saves a huge space when you have more than 10k records in database.

The optimization aspect is to store double float values as integers in database.

Share: