title: Laravel 4: Interating over array of objects
body: <p>I'm slowly learning Laravel/PHP and am building a menu system for a basic CMS.I have stored the menu in the database as json. This way I can use it for other apps etc.</p>
  
  <p>I am trying to now decode the json and loop through it to display my menu. The new array is an array of objects. My question is:</p>
  
  <p>• How can I iterate through this array? When I attempt to get $item->id it works fine. However, when I then try to get $item->title I get an ErrorException</p>
  
  <p>I'm sure it is something simple. Its driving me nuts! Thanks for take the time to look...</p>
  
  <h3>Controller</h3>
  
  <pre><code>public function edit($id)
  {
      $view = View::make('admin.menus.edit');
      $view['seo_title'] = Lang::get('admin.system_name') . ' | Edit ' . Lang::choice('admin.menus', 1);
      $view['id'] = $id;
      $view['menu'] = Menus::find($id);
      $view['menuitems'] = json_decode($view-&gt;menu-&gt;menu_items);
      return $view;
  }
  </code></pre>
  
  <h3>View with foreach</h3>
  
  <pre><code>    @foreach( $menuitems as $item)
      &lt;h4&gt;{{ $item-&gt;title }}&lt;/h4&gt;
  @endforeach
  </code></pre>
  
  <h3>Json in menu_items col</h3>
  
  <pre><code>[{"title":"Title","link":"/test.html","id":"1","children":[{"title":"Title","link":"/test.html","id":"2","children":[{"title":"Title","link":"/test.html","id":"3"}]}]},{"id":"4"},{"id":"5","children":[{"id":"6","children":[{"id":"7"}]}]},{"id":"8"},{"id":"9"}]
  </code></pre>
  
  <h3>Object after json_decode</h3>
  
  <pre><code>Array ( [0] =&gt; stdClass Object (
                          [title] =&gt; Title
                          [link] =&gt; /test.html
                          [id] =&gt; 1
                          [children] =&gt; Array ( [0] =&gt; stdClass Object (
                                                                          [title] =&gt; Title
                                                                          [link] =&gt; /test.html
                                                                          [id] =&gt; 2 [children] =&gt; Array (
                                                                                                      [0] =&gt; stdClass Object (
                                                                                                      [title] =&gt; Title
                                                                                                      [link] =&gt; /test.html
                                                                                                      [id] =&gt; 3
                                                                                                      )
                                                                                                  )
                                                                      )
                                              )
                          )
  
  [1] =&gt; stdClass Object (
                          [id] =&gt; 4
                          )
  
  [2] =&gt; stdClass Object (
                          [id] =&gt; 5
                          [children] =&gt; Array ( [0] =&gt; stdClass Object (
                                                                          [id] =&gt; 6
                                                                          [children] =&gt; Array (
                                                                                              [0] =&gt; stdClass Object (
                                                                                              [id] =&gt; 7
                                                                                              )
                                                                                          )
                                                                      )
                                              )
                          )
  
  [3] =&gt; stdClass Object (
                          [id] =&gt; 8
                          )
  
  [4] =&gt; stdClass Object (
                          [id] =&gt; 9 
                          )
  </code></pre>
  
  <p>)</p>
  
  <p>Thanks again - I hope someone can spot the error of my ways.</p>