40 Laravel ‘@’ Blade Directives You Might Not Know and How to Create Custom Ones

Chimeremze Prevail Ejimadu
10 min readJun 28, 2024
Photo by Mohammad Rahmani on Unsplash

Blade is Laravel’s powerful, simple templating engine. While many developers like you are familiar with basic directives like @if and @foreach, Blade offers many other less-known directives that can streamline your code and enhance its readability. This article covers 40 Blade directives you might not know about, including their usage and examples to make your Blade templates cleaner and more efficient.

Not just that, I will teach you how to create your own custom laravel Blade directives.

Let’s start with the normal and then move into the best. You are going to see many beautiful laravel blade directives that will really boost your application speed and efficiency. Let’s go.

Conditional Directives

1. @if, @elseif, @else, and @endif

Use these directives for conditional statements.

@if($user->isAdmin())
<p>Welcome, Admin!</p>
@elseif($user->isModerator())
<p>Welcome, Moderator!</p>
@else
<p>Welcome, User!</p>
@endif

2. @switch and @case

Use these directives for switch statements.

@switch($user->role)
@case('admin')
<p>Welcome…

--

--

Chimeremze Prevail Ejimadu

Laravel Developer + Writer + Entrepreneur + Open source contributor + Founder + Open for projects & collaborations. Hit FOLLOW ⤵