Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make li class active dynamically in laravel?

My HTML code is

   <div id="sidebar"><a href="#" class="visible-phone"><i class="icon icon-home"></i>Dashboard</a>
        <ul>
          <li class="active"><a href="{{route('ScamType.index')}}"><i class="icon icon-home"></i> <span>Scam Type</span></a> </li>
          <li> <a href="{{route('ScamDatabase.index')}}"><i class="icon icon-signal"></i> <span>Scam Database</span></a> </li>
          <li> <a href="{{route('ScamStory.index')}}"><i class="icon icon-inbox"></i> <span>Scam Story</span></a> </li>
          <li><a href="{{route('KeyWord.index')}}"><i class="icon icon-th"></i> <span>Keyword</span></a></li>
          <li><a href="{{route('Category.index')}}"><i class="icon icon-th"></i> <span>Category</span></a></li>
          <li><a href="{{route('SubCategory.index')}}"><i class="icon icon-th"></i> <span>Sub Category</span></a></li>
</ul>
</div>

Here i gave li class active as like in bootstrap and its not working, but i don't know how to give in laravel and i am very beginner of laravel, so please avoid minus votes and give me the right solution for it.. How Should i change my code to get li class active dynamically?

like image 606
Maniraj Murugan Avatar asked Dec 18 '22 08:12

Maniraj Murugan


1 Answers

You can use ternary operator. For example, you can check URI for the current route:

<li{{ request()->is('scam-types') ? ' class="active"' : '' }}>

You can also use * as wildcard:

<li{{ request()->is('scam-type-number-*') ? ' class="active"' : '' }}>

Or you can check route name:

<li{{ request()->route()->getName() === 'ScamType.index' ? ' class="active"' : '' }}>
like image 122
Alexey Mezenin Avatar answered Feb 07 '23 15:02

Alexey Mezenin