php - Multi-level navigation using bootstrap in laravel 5 -
i using laravel 5 framework.
i have categories table this:
i want make bootstrap menu multi-level if parent_id = id
this have tried far:
<ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">categories <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> @foreach( $category $cat ) @if( $cat->id === $cat->parent_id) <ul class="dropdown-menu" role="menu"> <li class="dropdown-submenu"> <a href="{{ url( '/category', safeurl::make( $cat->name ) ) }}" data-toggle="dropdown-toggle" aria-expanded="false">{{ $cat->name }} <ul class="dropdown-menu" role="menu"> <li> <a href="{{ url( '/category', [safeurl::make( $cat->name ), safeurl::make( $cat->name )] ) }}"></a> </li> </ul> </a> </li> </ul> @else <li> <a href="{{ url( '/category', safeurl::make( $cat->name ) ) }}">{{ $cat->name }}</a> </li> @endif @endforeach </ul> </li> </ul>
but results is, output 1 below other.
i want if parent_id = id
, should show sub category next particular id.
for example, in current example, id = 6
has parent_id = 4
, means, bootstrap menu should show sub-category link next clothing
.
update 1:
after answer submitted, cannot display sub category.
here's code that:
<li class="dropdown"> <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">categories <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> @foreach( $category $cat ) <li @if($cat->childs->count()) class="dropdown" @endif> <a href="javascript:void(0)" @if( $cat->childs->count() ) class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" @endif> {{ $cat->name }} @if( $cat->childs->count() ) <span class="caret"></span> @endif </a> @if( $cat->childs->count() ) <ul class="dropdown-menu" role="menu"> @foreach( $cat->childs $child ) <a href="{{url('/category', [safeurl::make($cat->name), safeurl::make($child->name)])}}"> {{ $child->name }} </a> @endforeach </ul> @endif </li> @endforeach </ul>
how achieve ?
one way add relation in model
public function childs() { return $this->hasmany('category', 'parent_id', 'id'); }
in controller select category parent_id==0
and in view can ask children:
@foreach( $category $cat ) <li @if($cat->childs->count()) class="dropdown" @endif> <a href="{{ url( '/category', safeurl::make( $cat->name ) ) }}" @if($cat->childs->count()) class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" @endif>{{ $cat->name }} @if($cat->childs->count()) <span class="caret"></span> @endif</a> @if($cat->childs->count()) <ul class="dropdown-menu" role="menu"> @foreach($cat->childs $child) <li><a href="{{ url( '/category', [safeurl::make( $child->name ), safeurl::make( $child->name )] ) }}"></a></li> @endforeach </ul> @endif </li> @endforeach
Comments
Post a Comment