Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alewa's avatar
Level 2

Add Active Class with slug in laravel

I want to add an active class to my side, but the active class request is with a slug web.php

Route::get('resource','ResourceController@index')->name('resource');
Route::get('resource/category/{category}','ResourceController@resourceByCategory')->name('category');

my resource.blade.php file

@foreach($categories as $category)
             <a href="{{ route('category',$category->slug) }}" class="list-group-item list-group-item-action {{ 'category',$category->slug == request()->path() ? 'active' : '' }}">{{ $category->name }}</a>
          @endforeach

Please how do i add the active class using the resquest route with the slug in laravel?

0 likes
2 replies
MichalOravec's avatar
Level 75
@foreach($categories as $category)
    <a href="{{ route('category', $category->slug) }}" 
        class="list-group-item list-group-item-action {{ route('category', $category->slug) == url()->current() ? 'active' : '' }}"
    >{{ $category->name }}</a>
@endforeach
2 likes

Please or to participate in this conversation.