【问题标题】:JSON API response from controller to View从控制器到视图的 JSON API 响应
【发布时间】:2019-09-15 23:52:15
【问题描述】:

我正在尝试将 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": []
}

从视图中删除:

array:3 [▼
  0 => {#284 ▶}
  1 => {#289 ▶}
  2 => {#287 ▶}
]

谁能帮我在不改变的情况下在我的视图中获取 Api 输出?

【问题讨论】:

    标签: php json laravel


    【解决方案1】:

    看来你是在混命名

    $response = json_decode($response);
    

    试试这个

    $r = json_decode($response);
    

    因为你是从 api 获取数据而不是尝试将数据收集到 laravel 集合中

    $collections = collect($r);
    

    现在可以查看了

    return view('account.incomplete_profile', compact('collections'));
    

    希望对你有帮助

    【讨论】:

    • 请在我的回答上打勾
    猜你喜欢
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2016-06-26
    相关资源
    最近更新 更多