Home > Enterprise >  How to return Integer in Laravel (not JSON)
How to return Integer in Laravel (not JSON)

Time:07-15

Hello Guys I Have This Text {price:100} How Can I Remove String And The Output Will Be //100

This Is Mycode

function each() {
    $Offer_price = Offer::select('price')->get();
    foreach ($Offer_price as $Price) {
        return  $Price; // I Want The Output 100 Not {price:100}
    }
}

CodePudding user response:

First I would use json_decode to get your string into a json object. And then I would access price attribute like that:

  return json_decode($Price)->{'price'};

CodePudding user response:

If you are sure it's a string, try:

return json_decode($Price, true)['price'];

But if objects are automatically converted to JSON (by your framework), try:

return $Price->price;
  • Related