I\'m trying to set active class in my list item, but it doesn\'t work.
My code in blade:
@foreach($data as $site)
You can try this as well
<a href="#" class="{{ (\Request::route()->getName() == 'put the address of your route here or your route name') ? 'active' : '' }}">
e.g:
<a href="#" class="{{ (\Request::route()->getName() == 'admin') ? 'active' : '' }}">
Note: I only test in Laravel 5.5
//my admin.app.blade file
<ul class="nav">
<li class="nav-item @yield('query')">
<a class="nav-link" href="{{route('query_page')}}">
<i class="material-icons">query_builder</i>
<p>Queries</p>
</a>
</li>
<li class="nav-item @yield('feedback')">
<a class="nav-link" href="{{route('feedback_page')}}">
<i class="material-icons">feedback</i>
<p>Feedback</p>
</a>
</li>
</ul>
//query.blade
@extends('admin.app')
@section('title', 'Query Manager')
@section('page-heading', 'Query Manager')
@section('query','active')
//feedback.blade
@extends('admin.app')
@section('title', 'Feedback Manager')
@section('page-heading', 'Feedback Manager')
@section('feedback','active')
There is also the following if you have named your route :
<li class="{{ Request::routeIs('admin') ? 'active' : '' }}">...</li>
Inspired from here
I like more to use this syntax to check the route since has not been mentioned.
<li>
<a href="#" class="nav-link {{ request()->routeIs('team*') ? 'active font-weight-bolder' : '' }}">Teams</a>
</li>
Two more things:
Adding an asterisk, without a dot as route name will target all the routes that start with team, teams, etc.
If you are using any CSS framework like Bootstrap or Tailwind you can add here also the classes to style the menu item, instead of writing some CSS for it.