【问题标题】:How to fetch JSON array in Laravel如何在 Laravel 中获取 JSON 数组
【发布时间】:2016-11-22 05:06:16
【问题描述】:

我有一个名为Templatemodel 和一个名为templatesmigration table,并且我在持有的mysql table 中有一个column nametemplatedata JSON 数组,在尝试在 view 中获取 data 时出现错误,

以下是我的控制器:

  public function get_template($id)
{
    $gettemplate = Template::findOrFail($id);
    return view('nitseditor.theme', ['gettemplate' => $gettemplate]);
}

我正在尝试这样获取对象:

@foreach($gettemplate as $template)
    <div class="branding">
         <h1 class="logo">
             <a href="index.html"><img src="{{ URL::asset($template->templatedata.content.logoimage) }}" alt="" width="25" height="26">NitsEditor</a>
         </h1>
    </div>
@endforeach

以下是我在表格中的JSON format

[{
    "content": {
            "logo": {
                    "logoimage": "img/home/nitseditorlogo.png",
                    "logolink": "index.html"
                    },
                "pages": [
                    {"pagename": "Mysite", "pagelink": "index.html"}, 
                    {"pagename": "Templates", "pagelink": "templates.html"},
                    {"pagename": "About Us", "pagelink": "aboutus.html"},
                    {"pagename": "Contact Us", "pagelink": "contactus.html"}
                        ]
                }
}]

我收到以下错误:

试图获取非对象的属性(视图:刀片的位置)

【问题讨论】:

  • 当您使用findOrFail($id) 获取它时它只有1 条记录时,您为什么要循环$gettemplate
  • @TheFallen 我是为pagenamepagelink 制作的,我的意思是,我需要循环播放。
  • 我的意思是,如果您循环 $gettemplate 这是一条记录,您将循环 Template 对象的属性,这些属性不是对象,这就是为什么你得到一个错误。
  • @TheFallen,即使我删除 @foreach 并尝试像这样访问 $gettemplate-&gt;templatedata-&gt;content-&gt;logo-&gt;logoimage 我也会遇到同样的错误。
  • 请解释什么是templatedata、content、logo、logoimage以及它们与Template对象的关系。

标签: php json laravel laravel-5.2


【解决方案1】:

试试这个兄弟

$data_string = '[{
    "content": {
            "logo": {
                    "logoimage": "img/home/nitseditorlogo.png",
                    "logolink": "index.html"
                    },
            "pages": [
                {"pagename": "Mysite", "pagelink": "index.html"}, 
                {"pagename": "Templates", "pagelink": "templates.html"},
                {"pagename": "About Us", "pagelink": "aboutus.html"},
                {"pagename": "Contact Us", "pagelink": "contactus.html"}
                ]
            }
}]';
$template = json_decode($data_string);
echo $template[0]->content->logo->logoimage.'<br>';
echo $template[0]->content->pages[0]->pagename;

结果

img/home/nitseditorlogo.png
Mysite

【讨论】:

  • 感谢更新,同时返回 $template 我得到 JSON,但是一旦我执行 $template->content 我得到同样的错误。
  • 我得到的 JSON 返回包含大括号 [{ }] 内的内容,您的 JSON 内容在 {} 内,无论我在哪里看到内容示例 [{ }] 我们必须放置一个数组,例如在上面的答案中你已经采取了pages[0],我尝试通过template[0]-&gt;content 调用,正如你在我的问题中看到的JSON 内容在[ ] 内现在我得到了错误The Response content must be a string or object implementing __toString(), "object" given.
  • 你的 JSON 是一个数组?对吗?
  • 是的,它是一个数组。
  • 找到了解决方案,感谢您的帮助。我已经编辑了答案!
猜你喜欢
  • 2021-05-12
  • 2017-03-15
  • 1970-01-01
  • 2019-10-06
  • 2017-11-05
  • 2018-06-23
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多