title: Laravel 4 - Eloquent. Infinite children into usable array?
body: <p>I've got a categories table. Each category can have an optional parent (Defaults to 0 if no parent).</p>
  
  <p>What I want to do is build a simple html list tree with the levels of the categories.</p>
  
  <p>Example date:</p>
  
  <pre><code>Foods
  -- Fruit
  ---- Apple
  ---- Banana
  ---- Orange
  -- Veg
  ---- Cucumber
  ---- Lettuce
  Drinks
  -- Alcoholic
  ---- Beer
  ---- Vodka
  Misc
  -- Household Objects
  ---- Kitchen
  ------ Electrical
  -------- Cooking
  ---------- Stove
  ---------- Toaster
  ---------- Microwave
  </code></pre>
  
  <p>Note that this needs to work for around 10 'levels'. I'd love it to be infinite but I really dont want to be going down the route of using a nested set model as it'll cause huge delays on this project.</p>
  
  <p>The docs on this for laravel are terrible, with no real reference as to where to even start. I've been playing with it for days trying to work out what to do and seem to be getting nowhere without a huge messy block of for each loops within each other 10 times.</p>
  
  <p>I've got my tree of data using the following in my model:</p>
  
  <pre><code>&lt;?php
  class Item extends Eloquent {
      public function parent()
      {
          return $this-&gt;hasOne('Item', 'id', 'parent_id');
      }
  
      public function children()
      {
          return $this-&gt;hasMany('Item', 'parent_id', 'id');
      }
  
      public function tree()
      {
          return static::with(implode('.', array_fill(0,10, 'children')))-&gt;where('parent_id', '=', '0')-&gt;get();
      }
  }
  </code></pre>
  
  <p>This gets all the parent and children up to a level of 10. This works fine, but you cant really then do anything with the child data without manually having 10 foreach loops within each other.</p>
  
  <p>What am I doing wrong here? Surely this shouldn't be this hard/poorly executed? All I want do do is get a simple html list with the items in a tree structure.</p>
  
  <p>I've put together a quick SQLFiddle example of the dummy data used above: <a href="http://sqlfiddle.com/#!2/e6d18/1">http://sqlfiddle.com/#!2/e6d18/1</a></p>