Home > Net >  How to set active class dynamically on foreach element laravel 9
How to set active class dynamically on foreach element laravel 9

Time:12-22

I want to set active class when i clicked it on foreach element, I have this code but its make all looping elements active, i just want to set active class when i click of element i clicked, like nav menu active but this case is in foreach element,

sorry my english is bad

here is my code

 <div >
     <h3 >Kategori</h3>
     <div >
          <a href="{{ route('shop') }}" >
             All Product
          </a>

          @foreach ($ctg as $c)
              <a href="{{ route('shop.category', $c->slug) }}"
                 >
                 {{ $c->name }}
              </a>
          @endforeach

     </div>
</div>

CodePudding user response:

Instead of:


If you use this, you can activate the category on the active page:

@class([
    'kategori-link',
    'active' => route('shop.category', $c->slug) == request()->url()
])

Add it like an attribute.

  • Related