skip to Main Content

i have an issue. If i give to my links routes, all of them becomes active

<li class="{{ (strpos(Route::currentRouteName(), 'dashboard') == 0) ? 'active' : '' }}">
<a href="{{ route('dashboard') }}">
<i class="uil uil-dashboard me-2 d-inline-block"></i>Dashboard</a></li>

Can somebody explain me why is that happening?

2

Answers


  1. strpos returns int(0) when Route::currentRouteName() is 'dashboard' and bool(false) otherwise.

    You’re using the == operator which is NOT strict comparison. Try using === instead.

    false == 0  // true
    false === 0 // false
    
    Login or Signup to reply.
  2. You should use the request()->route()->named() method :

    https://laravel.com/docs/10.x/routing#inspecting-the-current-route

    <li class="{{ request()->route()->named('dashboard') ? 'active' : '' }}">
        <a href="{{ route('dashboard') }}">
              <i class="uil uil-dashboard me-2 d-inline-block"></i>Dashboard
        </a>
    </li>
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search