如何在Laravel中获取JSON数组

时间:2016-07-19 10:42:33

标签: php json laravel laravel-5.2

model名称为Templatemigration table名称为templates,我column name {持有JSON数组的templatedata中的{1}},同时尝试在mysql table中抓取data我收到错误,

以下是我的控制员:

view

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

  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

我收到了以下错误:

  

尝试获取非对象的属性(查看:刀片的位置)

1 个答案:

答案 0 :(得分:3)

试试这个兄弟

$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