doucong7963
2016-07-19 10:42
浏览 302
已采纳

如何在Laravel中获取JSON数组

I've a model in the name of Template and migration table in the name of templates, and i'm having a column name of templatedata in the mysql table which is holding the JSON array, while trying to fetch data in the view I'm getting an error,

Following is my controller:

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

I'm trying to fetch the object like this:

@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

and following is my JSON format in table:

[{
    "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"}
                        ]
                }
}]

I'm getting following error:

Trying to get property of non-object (View: location of blade)

图片转代码服务由CSDN问答提供 功能建议

我在 Template 的名称中有 model templates 名称中的 migration table ,我在 templatedata 中有列名 持有JSON数组的代码> mysql表,同时尝试在视图中获取 data 我收到错误,

以下是我的控制器:

  public function get_template($ id)
 {
 $ gettemplate = Template :: findOrFail($ id); 
 返回视图('nitseditor.theme',['gettemplate'=&gt; $ gettemplate]); 
} 
   
 
 

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

  @foreach($ gettemplate as $ template)
&lt; div class =“branding”&gt; 
&lt; h1 class =“logo”&gt; \  n&lt; a href =“index.html”&gt;&lt; img src =“{{URL :: asset($ template-&gt; templatedata.content.logoimage)}}”alt =“”width =“25”height  =“26”&gt; NitsEditor&lt; / a&gt; 
&lt; /  h1&gt; 
&lt; / div&gt; 
 @ endforeach 
   
 
 

以下是表格中的 JSON格式

  [{
“content”:{
“logo”:{
“logoimage”:“img / home / nitseditorlogo.png”,
“logolink”:“index  .html“
},
”“页面”:[
 {“pagename”:“Mysite”,“pagelink”:“index.html”},
 {“pagename”:“模板”,“pagelink”  :“templates.html”},
 {“pagename”:“关于我们”,“pagelink”:“aboutus.html”},
 {“pagename”:“联系我们”,“pagelink”:“contactus。  html“} 
] 
} 
}] 
   
 
 

我收到以下错误:

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzouya5792 2016-07-19 10:48
    已采纳

    Try this bro

    $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;
    

    Result

    img/home/nitseditorlogo.png
    Mysite
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题