Skip to content
Advertisement

Laravel active menu item for url included parameters

I’m trying to set active class in my list item, but it doesn’t work.

My code in blade:

@foreach($data as $site)
  <ul class="sidebar-menu" id="second-menu">
  @if(isAdmin())<li class="{{-- active class  for url parameter --}}"><a href="{{ url('sites/'.$site->id.'/edit') }}" >{{ $site->name }}</a></li>
@endif
</ul>
@endforeach

So, if I write: li class=”@if(getRouteName() == ‘site@index’){{ ‘active’ }}@endif” , it works nice, but in my case the problem is that I want to get ‘active’ class in foreach sites/’.$site->id.’/edit

Many thanks.

Advertisement

Answer

Use is() method. For example:

<li class="{{ request()->is('sites/*/edit') ? 'active' : '' }}"
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement