How do I parse this json inside array data in view blade?

試著忘記壹切 提交于 2020-01-06 09:03:42

问题


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


来源:https://stackoverflow.com/questions/51765162/how-do-i-parse-this-json-inside-array-data-in-view-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!