'Laravel 8, response json return float value as string

I'm using query builder to get the data

DB::table('products')->get()

the results is

{"data" : [
   {
     "id": 1,
     "name": "Product A",
     "price": "100000"
   },
   {
     "id": 1,
     "name": "Product B",
     "price": "150000"
   }
  ]
}

how to cast the price back to float? I have used $casts = ["price" => "float"] but it doesn't work since I'm not using eloquent



Solution 1:[1]

"price": "150000"

The "price" is set as string. You need to change to number.

"price": 150000

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Duong Anh