Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel active menu item for url included parameters

Tags:

php

laravel

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.

like image 294
Arbnor Salihu Avatar asked May 12 '17 12:05

Arbnor Salihu


3 Answers

Use is() method. For example:

<li class="{{ request()->is('sites/*/edit') ? 'active' : '' }}"
like image 53
Alexey Mezenin Avatar answered Oct 18 '22 04:10

Alexey Mezenin


I am currently using Laravel 5.6.* and my solution is:

<a href="#" class="nav-link {{ request()->is('users*') ? 'active' : '' }}">Users</a>
like image 22
Him Hah Avatar answered Oct 18 '22 03:10

Him Hah


There is also the following if you have named your route :

<li class="{{ Request::routeIs('admin') ? 'active' : '' }}">...</li>

Inspired from here

like image 20
cetteSara Avatar answered Oct 18 '22 04:10

cetteSara