How to make li class active dynamically in laravel?

My HTML code

   <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 said that the li class is active, as in bootstrap, and it does not work, but I don’t know how to give in laravel, and I really start laravel, so please avoid minus votes and give me the correct solution for this .. How do I change my code to dynamically activate the li class?

+4
source share
3 answers

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

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

You can also use *as a wildcard:

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

Or you can check the name of the route:

<li{{ request()->route()->getName() === 'ScamType.index' ? ' class="active"' : '' }}>
+4
source

li URL

if (strpos($_SERVER['REQUEST_URI'], "ScamType") !== false){
   echo "active";
}

.

Ex.

<li class="{{ if (strpos($_SERVER['REQUEST_URI'], "ScamType") !== false){  echo "active"; } }}"><a href="{{route('ScamType.index')}}"><i class="icon icon-home"></i> <span>Scam Type</span></a> </li>
+2

, class="active" class=active

<li{{ request()->route()->getName() === 'user.index' ? ' class=active' : '' }}>
+1

Source: https://habr.com/ru/post/1674079/


All Articles