Home > Back-end >  Fix decimal being retrieved from Database as string in Laravel
Fix decimal being retrieved from Database as string in Laravel

Time:12-19

If I have a column like this 'price' as decimal, when I get this value from Database, it returns a string instead of decimal or float. How can I fix this?

CodePudding user response:

you can solve it in the whole project by casting in model

protected $casts = ['column_name' => 'type'];

example:

protected $casts = ['price' => 'float'];

documentation link: click here

CodePudding user response:

You can just do this before showing;

$price = (float)$product->price;

Or you can make a function, it will work.

  • Related