skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. You can just do this before showing;

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

    Or you can make a function, it will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search