Php 打印.blade模板中的对象数组
我正在使用Php 打印.blade模板中的对象数组,php,laravel,guzzle,Php,Laravel,Guzzle,我正在使用Guzzle向外部API发出HTTP请求,并获取一些数据作为如下响应: use GuzzleHttp\Client; $client = new Client(); $res = $client->get('https://jsonplaceholder.typicode.com/posts'); return view('home')->with('result', $res->getBody()); 但是我不知道如何在我的.blade模板中打印它。解决方法是什
Guzzle
向外部API发出HTTP请求,并获取一些数据作为如下响应:
use GuzzleHttp\Client;
$client = new Client();
$res = $client->get('https://jsonplaceholder.typicode.com/posts');
return view('home')->with('result', $res->getBody());
但是我不知道如何在我的.blade
模板中打印它。解决方法是什么?使用getContents()
方法获取响应数据,并使用json\u decode
解码json响应,如下所示:
use GuzzleHttp\Client;
$client = new Client();
$response = $client->get('https://jsonplaceholder.typicode.com/posts');
$posts = json_decode($response->getBody()->getContents());
return view('home', compact('posts));
在blade
文件中,您可以将它们呈现为如下列表项:
<ul>
@foreach($posts as $post)
<li> {{ $post->title }} </li>
@endforeach
</ul>
@foreach($posts作为$post)
- {{$post->title}
@endforeach
你能用print_r()打印它并更新你尝试的ArrayID,然后像foreach
一样循环吗?谁投了反对票,为什么?