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

如何在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)

  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥50 WPF Lidgren.Network.Core2连接问题
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M