简体   繁体   English

Laravel 5.1中的多级菜单-循环

[英]Multi-level menu in laravel 5.1 - loops

I am a little bit new with laravel 5.1 framework. 我对laravel 5.1框架有点陌生。 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. 在主菜单中,parent_id = 0的项。子菜单项的parent_id = id,以此类推。

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. 我想显示正确,但是当我将没有菜单项的主菜单项悬停时,会出现css块,因为如果条件不好,我就没有做好。 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. 另一个问题是如何从菜单模型中仅获取一个值,例如只能用于指向一个子菜单的id

Best regards! 最好的祝福!

You can do it simply by using recursion. 您可以简单地通过使用递归来做到这一点。 I removed html classes for readability. 我删除了html类以提高可读性。

Note: Set NULL parent_id for root items. 注意:为根项目设置NULL parent_id。

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. 这就是您的childItems.blade.php的外观。

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

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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