Home > Blockchain >  How to query nested object for individual data?
How to query nested object for individual data?

Time:10-05

I have a laravel 5 project and my guzzle response gives me this object:

{
 "people":{
    "sign":"AUT",
    "items":[
        {
            "name":{
                "William"
            },
            "stats":{
                "age": 46,
                "height": 181
            },
            "link":"google.com"
        },
        {
            "name":{
                "Eric"
            },
            "stats":{
                "age": 41,
                "height": 175
            },
            "link":"google.com"
        }
    ]
 }
}

I have issues accessing individual data, I want to display a foreach for the items and show individual data for each of them.

I attempted with object access:

<ul>
    @foreach($data as $item)
        <li>
            {{$item->items}}
        </li>
    @endforeach
</ul>

CodePudding user response:

<ul>
    @foreach($data->people->items as $item)
        <li>
            {{ $item }}
        </li>
    @endforeach
</ul>

CodePudding user response:

You are getting a JSON object as the response. I would recommend to convert it to a PHP array in your controller before doing anything.

$data = json_decode($response, true);

Then you can pass this array to the view and loop through it in your view.

  • Related