Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Blade directive to add true/false conditions

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

Before:

<div class="@if ($active) underline @endif">`

Now:

<div @class(['underline' => $active])>
@php
$isActive = false;
$hasError = true;
@endphp
 
<span @class([
'p-4',
'font-bold' => $isActive,
'text-gray-500' => ! $isActive,
'bg-red' => $hasError,
])></span>
 
<span class="p-4 text-gray-500 bg-red"></span>

Tip given by @Teacoders

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses