When And How To Use @class Blade Directive In Laravel

Hello artisan, in this laravel blade conditional class tutorial i am going to discuss about laravel @class blade directive. This @class directive conditionally compiles a CSS class string. For example, in Blade templates, if you want to add HTML classes conditionally, then you can do this by using the @class blade directive.

This @class directive accepts an array of classes where the array key contains the class or classes we wish to add, while the value has to be boolean expression. See the example:

Example code:

 

Read also : Best Way to Exclude Resourceful Functions in Laravel

 

Here $isActive is false. So it removes font-bold class from the HTML attributes.  the class() would only include the font-bold class when the $isActive is true. Hope it can help you.

 

#laravel #laravel-8x