How do I parse this json inside array data in view

2019-08-24 19:33发布

问题:

Currently this is my view

        {{ $data["id_user"] }} 

my controller

$client = new Client;
    $request = $client->get('url')->getBody()->getContents();  
    return view('Admin/lala')->with('data', json_decode($request, true));

get api

    {
  "code": 200,
  "data": [
    {
      "id_user": 1      
    }
  ]
}

I wanted to display it, I've tried it like in here but it's still an error. is there something wrong when I parse the data

回答1:

As in your json your id_user is inside array data so you have to use foreach in your blade.

Controller:

$client = new Client;
$request = $client->get('url')->getBody()->getContents();  
return view('Admin/lala')->with('data', json_decode($request, true));

you can also do it like this:

$client = new Client;
$request = $client->get('url')->getBody()->getContents();  
$data = json_decode($request, true);
return view('Admin/lala', compact('data'));

in your Blade file:

 // since it is not an array so you access code with out using foreach
 {{ $data['code'] }}

 //since id_user is in array so using foreach 
 @foreach($data['data'] as $json_d)

  {{ $json_d['id_user'] }}

 @endforeach