foreach laravel没有结果

时间:2019-09-01 13:51:57

标签: php laravel api

foreach API请求结果有问题。

获取数据的代码:

public function index() {

     if(!session()->has('access_token')) {
        return redirect('/login');
    }

    $accessToken = session()->get('access_token');

    $client = new Client();
    $response = $client->request('GET', env('API_URL').'/profile_question', [
        'headers' => [
            'Accept' => 'application/json',
            'Authorization' => 'Bearer '.$accessToken,
        ],
    ]);

    return view('incompleteprofile.incompletewizard', compact(['response']));

Foreach:

@foreach ($response as $swingstatus)
    <p>{{$swingstatus->Question}}</p>
@endforeach

结果转储:

[[{"id":1,"Question":"Swing status","description":"test","gender":"male","language":"en"},{"id":4,"Question":"Swing status","description":"test5","gender":"male","language":"en"}],[{"id":5,"Question":"Searching For","description":"teererer","gender":"male","language":"nl"}],[{"id":5,"Question":"Searching For","description":"teererer","gender":"male","language":"nl"}]]

刀片中的foreach结果保持空白,有人可以帮助我获取刀片中的数据。

1 个答案:

答案 0 :(得分:0)

您应该使用此代码`:

@foreach ($response[0] as $swingstatus)
   <p>{{$swingstatus->Question}}</p>
@endforeach

您的回复是一个array,其中array中有一个objects