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:
Property | Description |
---|---|
$loop->index | The index of the current loop iteration (starts at 0). |
$loop->iteration | The current loop iteration (starts at 1). |
$loop->remaining | The iterations remaining in the loop. |
$loop->count | The total number of items in the array being iterated. |
$loop->first | Whether this is the first iteration through the loop. |
$loop->last | Whether this is the last iteration through the loop. |
$loop->even | Whether this is an even iteration through the loop. |
$loop->odd | Whether this is an odd iteration through the loop. |
$loop->depth | The nesting level of the current loop. |
$loop->parent | When 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>© 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β) istrue
β- 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)