popolo - code simple..

popolo - code simple..

How to control html class depending on Laravel route

Theodoros Kafantaris's photo
Theodoros Kafantaris
·May 27, 2022·

1 min read

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>
 
Share this