Conditional Classes Blade Directives in Laravel - TechvBlogs

Conditional Classes Blade Directives in Laravel

The @class directive conditionally compiles a CSS class string. The directive accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 year ago

New in Laravel 8.51:  @classBlade directive to add true / false conditions on whether some CSS class should be added.

The  @class directive conditionally compiles a CSS class string. The directive accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression. If the array element has a numeric key, it will always be included in the rendered class list:

@php
    $hasError = true;
@endphp

<span @class([
    'p-4',
    'bg-red' => $hasError,
])></span>

Thank you for reading this blog.

Read Also:  How to Install React in Laravel 8

If you have any queries or doubts about this topic please feel free to  contact us . We will try to reach you.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.