问题
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:
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
回答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