Adding a class to make the current page “active” in your application’s navigation is a simple UI improvement. Let me show you how I typically do this in my apps.

Generally speaking, the easiest way to do this is comparing the name of the current route and the one you’d like to test against.

Luckily, Laravel’s Illuminate\Support\Facades\Route class provides a nice currentRouteName() method that we can use to make a comparison.

Let’s create a little function that you can re-use throughout your application.

use Illuminate\Support\Facades\Route;

function active_route(string $name): bool
{
    return Route::currentRouteName() === $name;
}

This function is great, but it doesn’t do anything with classes. Let’s add a second parameter so that a class string such as 'bg-gray-900' could be passed through.

#laravel

How to Add Classes to Active Link in Laravel
14.65 GEEK