如何解析视图刀片中数组数据中的json?
问题描述:
当前这是我的观点
{{ $data["id_user"] }}
我的控制器
$client = new Client;
$request = $client->get('url')->getBody()->getContents();
return view('Admin/lala')->with('data', json_decode($request, true));
获取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
答
在json中,您的 id_user 位于数组 data 内,因此您必须在刀片中使用foreach
As in your json your id_user is inside array data so you have to use foreach in your blade.
控制器:
$client = new Client;
$request = $client->get('url')->getBody()->getContents();
return view('Admin/lala')->with('data', json_decode($request, true));
您也可以这样:
$client = new Client;
$request = $client->get('url')->getBody()->getContents();
$data = json_decode($request, true);
return view('Admin/lala', compact('data'));
在您的Blade文件中:
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