Unable to get and display grandchild relationship for dynamic menu in laravel 5.8

被刻印的时光 ゝ 提交于 2021-02-11 13:41:48

问题


I am creating dynamic multilevel menu in laravel, I am able to get parent and child relationship and display the menu but unable to get and display if relationship goes one more level down.

Please assist.

My Model:

public function parent()
{
    return $this->belongsTo('App\Menu', 'parent_id');
}

public function children()
{
    return $this->hasMany('App\Menu', 'parent_id');
}

public function gchildren()
{
    return $this->hasMany('App\Menu', 'parent_id');
}

My Nav View:

<ul class="navbar-nav">
  @foreach($menus as $menuItem)
    @if( $menuItem->parent_id == 0 )
      <li class="nav-item p-relative">
        <a class ="nav-link" href="{{ $menuItem->children->isEmpty() ? $menuItem->url : "#" }}">
          {{ $menuItem->title }}
          @if( ! $menuItem->children->isEmpty() )
            <i class="fas fa-chevron-down"></i>
          @endif
        </a>
        @if( ! $menuItem->children->isEmpty() )
          <ul class="dropdown-menu">
            @foreach($menuItem->children as $subMenuItem)
              @if( ! $subMenuItem->status == 0 )
                <li class="nav-item">
                  <a class="nav-link" href="{{ $subMenuItem->url }}">{{ $subMenuItem->title }}</a>
                  @if( ! $menuItem->gchildren->isEmpty() )
                    <ul class="dropdown-menu">
                      @foreach($menuItem->gchildren as $gchildItem)
                        @if( ! $gchildItem->status == 0 )
                          <li class="nav-item">
                            <a class="nav-link" href="{{ $gchildItem->url }}">{{ $gchildItem->title }}</a>
                          </li>
                        @endif
                      @endforeach
                    </ul>
                  @endif
                </li>
              @endif
            @endforeach
          </ul>
        @endif
      </li>
    @endif
  @endforeach
</ul>

This is what I am getting:

MenuImage1

There should not be a next dropdown in above image.

In My DB there are Only 2 entries for parent ID 3 which is already a parent

db image

MenuImage2

MenuImage3


回答1:


Fixed the issue. Thanks all for you comments.

Removed gchildren function and changed code in nav view to this:

<ul class="dropdown-menu">
 @foreach($menuItem->children as $subMenuItem)
  @if( ! $subMenuItem->status == 0 )
   <li class="nav-item"><a class="nav-link" href="{{ $subMenuItem->url }}">{{ $subMenuItem->title }}</a>
    @if( ! $subMenuItem->children->isEmpty() )
     <ul class="dropdown-menu">
      @foreach($subMenuItem->children as $childItem)
       @if( ! $childItem->status == 0 )
        <li class="nav-item"><a class="nav-link" href="{{ $childItem->url }}">{{ $childItem->title }}</a></li>
       @endif
      @endforeach
     </ul>
    @endif
   </li>
  @endif
 @endforeach
</ul>


来源:https://stackoverflow.com/questions/57505484/unable-to-get-and-display-grandchild-relationship-for-dynamic-menu-in-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!