简体   繁体   中英

Multi-level menu in laravel 5.1 - loops

I am a little bit new with laravel 5.1 framework. Last couple of days I create my database (insert, update, delete) for dynamic menu I want to create. I connect with default layout in which i put menu. From route code looks like this.

View::composer('layouts.default',function($view){
    $menus = Menu::where('parent_id',0)->orderBy('order')->get();
    $submenus = Menu::where('parent_id','!=',0)->get();
    $view->with(compact('menus','submenus'));
});

In main menu are items with parent_id = 0. Submenu items have parent_id = id, and soo on.

I want to display correct but when I hover main menu items that dont have items, css block appear, becouse i didnt make good if condition. Is there any way to do this?

Code in view look like this.

@foreach($menus as $menu)
                    <li class="dropdown {!! (Request::is('typography') || Request::is('advancedfeatures') || Request::is('grid') ? 'active' : '') !!}"><a href="#" class="dropdown-toggle" data-toggle="dropdown"> {!! $menu->title !!}</a>
                        <ul class="dropdown-menu" role="menu">
                        @foreach($submenus as $submenu)
                                @if($submenu->parent_id === $menu->id)
                                <li><a href="{{ URL::to('typography') }}">{!! $submenu->title !!}</a>
                                    @foreach($submenus as $smenu)
                                            @if($smenu->parent_id === $submenu->id)
                                                <ul class="dropdown-submenu" role="menu">
                                                    <li><a href="{{ URL::to('typography') }}">{!! $smenu->title !!}</a>
                                                    </li>
                                                </ul>
                                            @endif
                                    @endforeach
                                </li>
                            @endif
                            @endforeach
                        </ul>
                    </li>
                    @endforeach

One more question is how to take only one value from Menu model for example id that can be used to point only one submenu.

Best regards!

You can do it simply by using recursion. I removed html classes for readability.

Note: Set NULL parent_id for root items.

Add this relation to your Menu Model.

function childs()
{
    return $this->hasMany('Namespace\Menu','parent_id', 'id');
}

In your controller get the menus who has no parent.

  $menus = Menu::whereNull('parent_id')->orderBy('order')->get();

Then display them in your view.

<ul>
  @foreach($menus as $menu)
     <li>
         {!! $menu->title !!}
         @include('childItems')//recursion view
     </li>
 @endforeach
</ul>

And this is what your childItems.blade.php will look like.

 <ul>
    @foreach($menu->childs as $menu)
       <li>
          {!! $menu->title !!}
          @include('childItems')//call itself for deeper relations.
       </li>
    @endforeach
 </ul>

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM