Laravel active menu item for url included parameters

前端 未结 10 2318
暖寄归人
暖寄归人 2020-12-28 14:43

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

My code in blade:

@foreach($data as $site)
  
相关标签:
10条回答
  • 2020-12-28 15:23

    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

    0 讨论(0)
  • 2020-12-28 15:23
    //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')
    
    0 讨论(0)
  • 2020-12-28 15:24

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

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

    Inspired from here

    0 讨论(0)
  • 2020-12-28 15:25

    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.

    0 讨论(0)
提交回复
热议问题