Laravel 5.6 api响应

时间:2018-03-15 13:44:48

标签: php laravel api controller

我的API会返回多个帐户objects,我想在view中列出,但我不知道如何。 有人能帮助我吗?

方式

   public function get(){
    $client = new  client([
        'headers' => ['content-type' => 'application/json' , 'Accept' => 'application/json'],
    ]);
  $response = $client->request('GET','https://lusp.com/api/login');

  $data = $response->getBody();
  $data = json_decode($data);

  dd($data);


}

响应

    {#491 ▼
      +"accounts": array:2[▼
        0 => {#465 ▼
          +"id": 7
          +"email": "Ivan@mail.com"
          +"password": "4154512"
        }
        1 => {#466 ▼
          +"id": 1
          +"email": "lucas@mail.com.br.br.br"
          +"password": "lucas123"
        }
]
}

SF

1 个答案:

答案 0 :(得分:1)

dd不理想, 试着像这样返回它

$data = collect(son_decode($data));
return response()->json($data);

如果您将其返回到视图中,请执行以下操作

$data = collect(json_decode($data));
return View("yourviewname",$data);

和yourviewname.blade.php

@foreach($account as $line)
 <span> {{$line->email}} </span>
@endforeach