Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
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?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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"' : '' }}>
about 4 years ago · Santiago Trujillo Report

0

Try Below Code to write each li tag for check some text in URL

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

Write above code in class.

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>
about 4 years ago · Santiago Trujillo Report

0

From above answer I have try and doesnt work because class="active" must be class=active

<li><a class="{{ request()->routeIs('about.index*') ? 'active-menu' : '' }}" href="{{route('about.index')}}"><i class="fa fa-desktop"></i> About</a></li>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!