Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从控制器到视图的JSON API响应_Php_Json_Laravel - Fatal编程技术网

Php 从控制器到视图的JSON API响应

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',

我试图从控制器获取传递JSON响应到视图,但JSON数组已更改。 如何传递从Api获得的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'));

希望这有帮助

请勾选我的答案