1

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)

7
  • Why are you looping $gettemplate when it's only 1 record when you fetch it with findOrFail($id)? Commented Jul 19, 2016 at 10:45
  • @TheFallen I made it for pagename and pagelink, I mean, for that I need to loop. Commented Jul 19, 2016 at 10:47
  • My point is that if you loop $gettemplate which is a single record, you'll be looping the properties of the Template object, which are not objects and that's why you're getting an error. Commented Jul 19, 2016 at 10:50
  • @TheFallen, even if I remove @foreach and try accessing like this $gettemplate->templatedata->content->logo->logoimage I'm getting the same error. Commented Jul 19, 2016 at 10:53
  • Please explain what are templatedata, content, logo, logoimage and how are they related to the Template object. Commented Jul 19, 2016 at 10:54

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the update while returning $template I get the JSON, but as soon as I do $template->content I get the same error.
I'm getting the JSON return having the content inside big bracket [{ }], your JSON contents are within {}, wherever I've seen the example for the contents [{ }] we have to place an array, for example in the above answer you have taken pages[0], I tried calling through template[0]->content, as you can see in my question the JSON contents are within [ ] now I'm getting the error The Response content must be a string or object implementing __toString(), "object" given.
Your JSON is an array? Right?
Yes, it is an array.
found out the solution, thanks for the help. I've edited the answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.