title: Laravel returning children of parents in same table
body: <p>Using Laravel 5.1, I am trying to create a menu list from a MySQL categories table. My service provider returns data, but I don't understand how to create the child categories in a foreach loop. When I perform the loop, only the last row of the child query is returned. Any guidance would be appreciated.</p>
  
  <p><strong>categories Table</strong></p>
  
  <pre><code>id  | cat_name      | cat_parent_id
  --- | --------------| ------------- 
  1   | Parent Cat 1  | NULL 
  2   | Parent Cat 2  | NULL 
  3   | Child Cat 1   | 2 
  4   | Child Cat 2   | 2 
  5   | Parent Cat 3  | NULL 
  6   | Child Cat 3   | 5
  </code></pre>
  
  <p><strong>Desired Result</strong></p>
  
  <pre><code>Parent Cat 1
  Parent Cat 2
      Child Cat 1
      Child Cat 2
  Parent Cat 3
      Child Cat 3
  </code></pre>
  
  <p><strong>viewComposerServiceProvider.php</strong></p>
  
  <pre><code>public function boot()
  {
         $this-&gt;composeTopCategoryNavigation();
         $this-&gt;composeSubCategoryNavigation();
  }
  
  private function composeTopCategoryNavigation()
  {
      view()-&gt;composer('partials.header', function($view)
          {
              $view-&gt;with('top_cats', Category::whereNull('cat_parent_id')-&gt;orderBy('cat_name', 'asc')-&gt;get());
          });
  }
  
  private function composeSubCategoryNavigation()
  {
          view()-&gt;composer('partials.header', function($view)
          {
              $view-&gt;with('sub_cats', Category::whereNotNull('cat_parent_id')-&gt;orderBy('cat_name', 'asc')-&gt;get());
          });
  }
  </code></pre>
  
  <p><strong>header view</strong></p>
  
  <pre><code>&lt;ul&gt;
  @foreach ($top_cats as $top_cat)
    &lt;?php $top_cat_slug = str_slug( $top_cat-&gt;cat_name, "-"); ?&gt;
    &lt;li&gt;{{ $top_cat-&gt;cat_name }}
      @foreach ($sub_cats as $sub_cat)
              @if ( $sub_cat-&gt;cat_parent_id === $top_cat-&gt;id )
                  &lt;ul&gt;
                    &lt;li{{ $sub_cat-&gt;cat_name }}&lt;/li&gt;
                  &lt;/ul&gt;
                  @endif
          @endforeach
     &lt;/li&gt;
  @endforeach
  &lt;/ul&gt;
  </code></pre>