title: Sidebar with dropdown menu from database
body: <p>I'm trying to use a database to add menu items on a sidebar on my site. My database is structured as follows: </p>
  
  <pre><code>ID / Product_Name/ Product_Type
  1 / product1 / type1
  2 / product2 / type1
  3 / product3 / type2
  4 / product4 / type2
  </code></pre>
  
  <p>I need a sidebar which has each type listed as a menu item, with the products belonging to that type as the submenu items:</p>
  
  <pre><code>  Type1
      product1 
      product2 
    Type2
      product3
      product4
  </code></pre>
  
  <p>With my current code however I achieve each menu item but my submenu shows every product in the entire database instead of just the products belonging to that type.</p>
  
  <p>My controller:</p>
  
  <pre><code>public function index()
  {
  
      //get all products
      $products = Product::all();  
  
      //get each type
      $types = Product::distinct()-&gt;get(['Type']);
  
      return view('pages.getstarted')-&gt;with('products', $products)                                       
                                     -&gt;with('types', $types);
  }
  </code></pre>
  
  <p>My view:</p>
  
  <pre><code>&lt;ul class="list-sidebar"&gt;                                
    @foreach($types as $type)
      &lt;li class="header"&gt;
        &lt;a href="#" data-toggle="collapse" data-target="#1"&gt;{{$type-&gt;Type}}&lt;span class="fa fa-chevron-left pull-right"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;ul class="sub-menu collapse" id="1"&gt;
          @foreach($products as $product)
            &lt;li&gt;&lt;a href="1"&gt;{{$product-&gt;Product_Name}}&lt;/a&gt;&lt;/li&gt;
          @endforeach
        &lt;/ul&gt;
      &lt;/li&gt;   
    @endforeach 
  &lt;/ul&gt;    
  </code></pre>
  
  <p>I am very new to Laravel so any help would be appreciated. </p>