TIP 23 / 73

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:

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

Now:

code
<div @class(['underline' => $active])>
php
@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>