Php 从控制器到视图的JSON API响应
我试图从控制器获取传递JSON响应到视图,但JSON数组已更改。 如何传递从Api获得的JSON数组 控制器代码:Php 从控制器到视图的JSON API响应,php,json,laravel,Php,Json,Laravel,我试图从控制器获取传递JSON响应到视图,但JSON数组已更改。 如何传递从Api获得的JSON数组 控制器代码: $client = new Client(); $response = $client->request('GET', env('API_URL').'/profile_question', [ 'headers' => [ 'Accept' => 'application/json',
$client = new Client();
$response = $client->request('GET', env('API_URL').'/profile_question', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
],
])->getBody();
$response = json_decode($response);
return view('account/incomplete_profile', compact(['response']));
控制器中的dd结果:
{#273 ▼
+"swingstatus": array:3 [▶]
+"searchingfor": []
+"language": []
+"headline": []
+"description": []
+"lookingfor": []
+"birthday": []
+"eyes": []
+"haircolor": []
+"height": []
+"weight": []
+"bodytype": []
+"raceethnics": []
+"smokes": []
+"piercings": []
+"tattoos": []
+"sexuality": []
+"hasexperience": []
+"looksareimportant": []
+"integlligence": []
+"hairlength": []
+"bodyhair": []
+"comfortlevel": []
+"whatdoyoulike": []
+"fetish": []
}
视图中的dd:
array:3 [▼
0 => {#284 ▶}
1 => {#289 ▶}
2 => {#287 ▶}
]
有人能帮我在我的视图中获得Api输出而不进行更改吗?看来你在混合命名
$response = json_decode($response);
试试这个
$r = json_decode($response);
而且因为您是从api获取数据,而不是尝试将数据收集到laravel collection中
$collections = collect($r);
现在好了,开始吧
return view('account.incomplete_profile', compact('collections'));
希望这有帮助请勾选我的答案