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() == ‘[email protected]’){{ ‘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.

Answer

Use is() method. For example:

<li class="{{ request()->is('sites/*/edit') ? 'active' : '' }}"

Leave a Reply

Your email address will not be published. Required fields are marked *