Php 在laravel刀片上显示实时json数据

Php 在laravel刀片上显示实时json数据,php,laravel,api,Php,Laravel,Api,我有实时json,我想在laravel blade上显示它 我的控制器代码是: public function index() { $json = json_decode(file_get_contents('link_of_json_file'),true); return view('show' , compact('json')); } @foreach ($json as $p) {{ $p->name }}

我有实时json,我想在laravel blade上显示它

我的控制器代码是:

public function index()
{
    $json = json_decode(file_get_contents('link_of_json_file'),true);
    return view('show' , compact('json'));
}
        @foreach ($json as $p)
            {{ $p->name }}
          @endforeach
刀片代码为:

public function index()
{
    $json = json_decode(file_get_contents('link_of_json_file'),true);
    return view('show' , compact('json'));
}
        @foreach ($json as $p)
            {{ $p->name }}
          @endforeach
这个错误显示为:

(2/2)错误异常

正在尝试获取非对象的属性(视图: E:\work landingpage\test\JSON\resources\views\show.blade.php)

您可以删除“true”以将其用作对象:

 public function index()
{
    $json = json_decode(file_get_contents('link_of_json_file'));
    return view('show' , compact('json'));
}

@foreach ($json as $p)
        {{ $p->name}}
      @endforeach
因此,如果您想将其用作数组,您可以像这样使用它:

 @foreach ($json as $p)
        {{ $p['name']}}
      @endforeach

我们不知道json文件的
link\u的内容是什么。它是一个对象数组吗?您在json_decode第二个参数中传递了true,这意味着它是关联数组。您可以像$p['name']那样访问它;当我删除“true”时,其工作方式为“true”。显示此错误消息时,无法将stdClass类型的对象用作array@GhyathDarwish不需要的时候不要评论自己的帖子。您可以简单地编辑答案@M.ramadan我编辑了我的答案,请查看它以了解我删除“true”的想法如果我将“true”保留在数组$p['name']@M.ramadan中,则此选项不起作用,并且显示无法使用stdClass类型的对象作为数组如果您仍然希望这样访问
$p->name
,则删除true!