title: Dropdown nested categories with unlimited subcategories -> Ajax / Laravel
body: <p>Im trying to create Nested Dropdown categories using the following code:</p>
  
  <p>My DB-> Table product_categories with fields:
  -id
  -categoryName
  -parentId-level
  -is_active</p>
  
  <p>My Product Entity to retrieve categories:</p>
  
  <pre><code>    public function getAjaxGetCategoriesDropdown() {
          $categories = DB::table('product_categories')
              -&gt;where('is_active', 1)
              -&gt;orderBy('path', 'asc')
              -&gt;select('categoryName','level','id','parentId')
              -&gt;get();
  
      $first = array();
      $children = array();
      foreach ($categories as $cat) {
          if ($cat-&gt;level == 2) {
              $first[$cat-&gt;id] = $cat;
          } else if ($cat-&gt;parentId) {
              $children[$cat-&gt;parentId][] = $cat;
          }
      }
      return array('first' =&gt; $first, 'children' =&gt; $children);
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public function create()
  {
      $cats = $this-&gt;product-&gt;getAjaxGetCategoriesDropdown();
      return View('products/create_product')-&gt;with(compact('products','cats'));
  }
  </code></pre>
  
  <p>In View: (create.blade.php)</p>
  
  <pre><code>&lt;div class="section"&gt;
    &lt;label class="field select"&gt;
          &lt;select id="first_cat" class="required-entry validate-select" onchange="showCat(this, 2)"&gt;
        &lt;option value=""&gt; Select a Category &lt;/option&gt;
          &lt;?php foreach ($tree['first'] as $cat): ?&gt;
        &lt;option value="&lt;?php echo $cat-&gt;id ?&gt;"&gt;&lt;?php echo $cat-&gt;categoryName ?&gt;&lt;/option&gt;
  &lt;?php endforeach ?&gt;
  &lt;/select&gt;
  &lt;i class="arrow double"&gt;&lt;/i&gt;
  &lt;/label&gt;
  &lt;div id="sub_cat"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My Script:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var children = $H(&lt;?php echo json_encode($tree['children']) ?&gt;);
  
      function showCat(obj, level) {
          var catId = obj.value;
          level += 1;
          if ($('cat_container_' + level)) {
              $('cat_container_' + level).remove();
          }
          if (children.get(catId)) {
              var options = children.get(catId);
              var html = '&lt;select id="cat_' + catId + '" onchange="showCat(this, ' + level + ')"&gt;' + '&lt;option value=""&gt;Select a SubCategory&lt;/option&gt;';
              for (var i = 0; i &lt; options.length; i++) {
                  html += '&lt;option value="' + options[i].entity_id + '"&gt;' + options[i].name + '&lt;/option&gt;';
              }
              html += '&lt;/select&gt;' + '&lt;i class="arrow double"&gt;&lt;/i&gt;';
              html = '&lt;label class="field select ' + 'cat_container_' + level + '"&gt;' + html + '&lt;/label&gt;';
  
              $('sub_cat').insert(html);
          }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>The current error that the Log displays says:</p>
  
  <blockquote>
    <p>Undefined Variable Tree in create.blade.php</p>
  </blockquote>
  
  <p>.. code <code>&lt;?php foreach ($tree['first'] as $cat): ?&gt;</code></p>
  
  <p>I've been using this code with success on a Magento app, but now porting the same approach to Laravel is not showing results. Any help appreciated.</p>