Skip to main content

"Black Friday" Discount! Only until December 1st: 50% off all membership plans!

Read more here
Quick Tip

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

Enjoyed This Tip?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses on Laravel Daily

Laravel Boost: Deeper Practical Dive

8 lessons
32 min

Marketing for Developers in 2026

7 lessons
52 min

Next.js Basics for Laravel Developers

11 lessons
58 min