Skip to content

Blade Directive Reference

Laravel Blade Directives Reference (Laravel 12)

A comprehensive list of all core Blade directives in Laravel 12, grouped and documented with examples.


🧠 Control Structures

Conditional rendering blocks.

@if, @elseif, @else, @endif

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

@unless, @endunless

Render the block if the condition is false.

@unless($user->isVerified())
<p>Please verify your account.</p>
@endunless

@isset, @endisset

Check if a variable is set and not null.

@isset($post->title)
<h1>{{ $post->title }}</h1>
@endisset

@empty, @endempty

Check if a variable is empty.
@empty($comments)
<p>No comments yet.</p>
@endempty

@switch, @case, @break, @default, @endswitch

Switch-style conditionals.

@switch($status)
@case('draft')
<p>Draft mode</p>
@break
@case('published')
<p>Published</p>
@break
@default
<p>Unknown</p>
@endswitch

@auth, @endauth

Render if user is authenticated.

@auth
<p>Welcome back!</p>
@endauth

@guest, @endguest

Render if user is not authenticated.

@guest
<p>Please log in.</p>
@endguest

@can, @cannot, @endcan, @endcannot

Check if user is authorized or unauthorized to perform an action.

@can('update', $post)
<button>Edit</button>
@endcan

@canany, @endcanany

Check if user has any of the given abilities.

@canany(['update', 'delete'], $post)
<button>Manage Post</button>
@endcanany

πŸ” Looping Constructs

@for, @endfor

Standard for-loop.

@for ($i = 0; $i < 5; $i++)
<li>{{ $i }}</li>
@endfor

@foreach, @endforeach

Iterate over arrays or collections.

@foreach ($users as $user)
<p>{{ $user->name }}</p>
@endforeach

$loop property

The $loop variable also contains a variety of other useful properties:

PropertyDescription
$loop->indexThe index of the current loop iteration (starts at 0).
$loop->iterationThe current loop iteration (starts at 1).
$loop->remainingThe iterations remaining in the loop.
$loop->countThe total number of items in the array being iterated.
$loop->firstWhether this is the first iteration through the loop.
$loop->lastWhether this is the last iteration through the loop.
$loop->evenWhether this is an even iteration through the loop.
$loop->oddWhether this is an odd iteration through the loop.
$loop->depthThe nesting level of the current loop.
$loop->parentWhen in a nested loop, the parent’s loop variable.

@forelse, @empty, @endforelse

Like @foreach, but handles empty lists.

@forelse ($posts as $post)
<li>{{ $post->title }}</li>
@empty
<li>No posts found.</li>
@endforelse

@while, @endwhile

Standard while-loop.

@while($i < 5)
<p>{{ $i++ }}</p>
@endwhile

@continue, @break

Control loop execution to skip or exit early.

@foreach ($items as $item)
@continue($item->isHidden())
<div>{{ $item->name }}</div>
@endforeach

πŸ“„ Template Inheritance & Sections

@extends, @section, @yield, @parent, @show

Define and extend layouts and sections.

This was confusing to me so I documentated the differences.

Note: While @section and @slot may seem similar because both allow injecting content, they serve different purposes and exist in different rendering contexts:

  • @section / @yield are used for template inheritance and define sections within layouts.
  • @slot is used for component composition, passing named pieces of content into Blade components.

They do not replace each other and operate in separate rendering trees.

@extends('layouts.app')
@section('content')
<p>Main content goes here.</p>
@endsection

You can alsop the following:

@extends('layouts.app')

to specify a Blade layout.

You can also set a layout when rendering a view in a controller or route using

view('my-view')->layout('layouts.app')

in Laravel environments that support it. This is most commonly used in Livewire or Volt components, where layout inheritance is defined programmatically rather than directly in the Blade file.

As of Laravel 10.23+, the same can also be done using

<?php layout('layouts.app'); ?>

at the top of your view file.

Here’s a simple example of using a layout with a section:

layouts/app.blade.php

<html>
<head>
<title>My App</title>
</head>
<body>
<header>
<h1>My Site</h1>
</header>
<main>
@yield('content')
</main>
@hasSection('footer')
<footer>
@yield('footer')
</footer>
@endif
</body>
</html>

home.blade.php

@extends('layouts.app')
@section('content')
<h2>Welcome to the Home Page</h2>
<p>This is the main content.</p>
@endsection
@section('footer')
<p>&copy; 2025 My Company</p>
@endsection

Explanation:

  • @section(β€˜content’) defines a block to inject into the @yield(β€˜content’) in the layout.
  • @section(β€˜footer’) will only be rendered if @hasSection(β€˜footer’) is true β€”
  • a useful pattern for optional page sections like footers or sidebars.

@hasSection, @sectionMissing

Check for the presence or absence of a section.

@hasSection(β€˜name’) checks if a section has been defined in the current view. Useful in layouts to conditionally render content if a child view provides a specific section.

@hasSection('sidebar')
@yield('sidebar')
@endif

🧩 Components & Slots

@component, @slot, @endcomponent, @endslot

Render reusable Blade components and slots.

@component('components.alert')
@slot('title') Alert Title @endslot
This is the alert content.
@endcomponent

@props, @aware

Declare component properties and inherit data.

@props(['type' => 'info'])
<div class="alert alert-{{ $type }}">
{{ $slot }}
</div>

πŸ“š Includes

@include, @includeIf, @includeWhen, @includeUnless, @includeFirst

Include other Blade views conditionally.

@include('partials.header')
@includeWhen($loggedIn, 'partials.user-menu')

πŸ“¦ Stacks & Pushes

@push, @prepend, @stack

Manage stacks of content, useful for scripts/styles.

@push('scripts')
<script src="custom.js"></script>
@endpush
@stack('scripts')

πŸ” Authentication & Authorization

(See Control Structures section for related directives like @auth, @can, etc.)


🌍 Localization

@lang, @trans, @choice

Translate strings and handle pluralization.

<p>@lang('welcome.title')</p>
<p>@choice('messages.apples', 5)</p>

βš™οΈ Helpers & CSRF

@csrf, @method, @php, @dd, @dump, @inject

Helpers for security, method spoofing, PHP, debugging, and service injection.

<form method="POST">
@csrf
@method('PUT')
</form>
@php($name = 'Taylor')
@dd($user)

🌱 Environment Checks

@env, @production, @once

Render based on environment or only once.

@env('local')
<p>You're in local environment.</p>
@endenv
@once
<script>alert('Loaded once!');</script>
@endonce

⚑ Livewire & Reactive

@livewire, @livewireStyles, @livewireScripts, @livewireStylesScripts, @entangle

Directives to include Livewire components and assets.

@livewire('counter')
@livewireStylesScripts

πŸ†• Laravel 12 Additions

@js

@js(['user' => $user])

Renders:

<script>window.data = {"user": { ... }}</script>

@fragment

@fragment('profile')
<x-profile :user="$user" />
@endfragment

Enhanced Directives

<input type="checkbox" @checked($isTrue)>
<option @selected($isSelected)>Item</option>

βž• Custom Directives

Register a directive:

Blade::directive('datetime', fn($exp) => "<?php echo ($exp)->format('m/d/Y H:i'); ?>");

Use:

@datetime($post->created_at)

πŸ“Œ Tip

Use php artisan view:clear to clear compiled Blade templates.


Updated for Laravel 12 (Released Feb 2025)