How to control html class depending on Laravel route
Many times, we are facing the issue to assign a different CSS class depending on which page we are visiting. I present 2 ways to do this.
- Using php ternary operator in Blade file
<a href="{{route('authentication')}}"
class="{{ request()->routeIs('authentication') ?
"text-blue-600" :
"text-gray-600" }}">
Authentication
</a>
- Using conditional classes
@class
in Blade file
<a href="{{route('authentication')}}"
@class([
"text-blue-600" => request()->routeIs('authentication'),
"text-gray-600" => !request()->routeIs('authentication')
])>
Authentication
</a>