dimanche 29 avril 2018

Sidebar with dropdown menu from database

Im trying to use a database to add menu items on a sidebar on my site. My database structure is the following:

ID / Product_Name/ Product_Type
1 / product1 / type1
2 / product2 / type1
3 / product3 / type2
4 / product4 / type2

I need a sidebar which has each type listed as a menu item, with the products belonging to that type as the submenu items:

  Type1
    product1 
    product2 
  Type2
    product3
    product4

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.

My controller:

     public function index()
{

    //get all products
    $products = Product::all();  

    //get each type
    $types = Product::distinct()->get(['Type']);


    return view('pages.getstarted')->with('products', $products)                                       
                                   ->with('types', $types);

}

My view:

    <ul class="list-sidebar">                                

          @foreach($types as $type)

        <li class="header"> <a href="#" data-toggle="collapse" data-target="#1"><span class="fa fa-chevron-left pull-right"></span> </a>
                <ul class="sub-menu collapse" id="1">
                @foreach($products as $product) <li><a href="1"></a></li> @endforeach
                </ul>
              </li>   

          @endforeach 

        </ul>    

I am very new to Laravel so any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire