Laravel Blade is one of the most powerful and advanced templating engine as compared to other templating engines such as Smarty and Twig.
- Control Structures
@if, @elseif, @else, @endif
Conditional logic.
@if ($user->isAdmin()) Welcome, admin! @elseif ($user->isModerator()) Welcome, moderator! @else Welcome, guest! @endif
@unless / @endunless
Reverse of @if.
@unless ($user->isBanned()) You can access the forum. @endunless
@isset / @endisset
Checks if a variable is set.
@isset ($profile) {{ $profile->bio }} @endisset
@empty / @endempty
Checks if a variable is empty.
@empty ($notifications) You have no notifications. @endempty
- Loops
@for / @endfor
Classic for-loop.
@for ($i = 0; $i < 5; $i++) Number: {{ $i }} @endfor
@foreach / @endforeach
For collections or arrays.
@foreach ($users as $user) {{ $user->name }} @endforeach
@forelse / @empty / @endforelse
foreach with a fallback if empty.
@forelse ($comments as $comment) {{ $comment->body }} @empty No comments found. @endforelse
@while / @endwhile
Basic while loop.
@while ($count < 3) Count: {{ $count++ }} @endwhile
- Authentication & Authorization
@auth / @endauth
Content for authenticated users.
@auth Welcome back, {{ auth()->user()->name }}! @endauth
@guest / @endguest
Content for unauthenticated users.
@guest <a href="/login">Login</a> @endguest
@can / @endcan / @cannot / @endcannot
Laravel policies or gates.
@can('update', $post) <a href="#">Edit Post</a> @endcan
@cannot('delete', $comment) You cannot delete this comment. @endcannot
@canany / @endcanany
If the user has any of the listed abilities.
@canany(['update', 'delete'], $post) <p>You can edit or delete this post.</p> @endcanany
- Environment & Section Management
@env / @endenv
Conditionally render for environments.
@env('local') <p>App is running in local</p> @endenv @env(['staging', 'production']) <p>Staging or Production</p> @endenv
@production / @endproduction
Production environment.
@production <p>Production environment</p> @endproduction
@section / @endsection
Define content sections for layouts.
@section('content') <p>This is the page content</p> @endsection
@yield
Used in layouts to show section content.
<body> @yield('content') </body>
@extends
Extend a layout.
@extends('layouts.app')
@include
Include another view.
@include('partials.header') @include('partials.sidebar', ['menu' => $menu])
@includeIf / @includeWhen / @includeUnless
Conditional includes.
@includeIf('partials.footer') @includeWhen($showSidebar, 'partials.sidebar') @includeUnless($isGuest, 'partials.user-info')
@each
Loop through and render a partial.
@each('partials.comment', $comments, 'comment')
@stack / @push
The stack directive allows you to render content somewhere else in another view or layout. By using the push directive you can add content to a stack. This is often used to render JavaScript and CSS includes.
For example, on the blog detail view we could push a JavaScript file that needs to be included. We can do this by pushing the HTML to the scripts stack.
@push('scripts') <script src="/blog-detail.js"></script> @endpush
In the <head> tag of the layout we could render the complete stack contents by using the stack directive and passing the name of the stack, scripts in this case.
<head> <!-- Your head content --> @stack('scripts') </head>
- Components & Slots
@component / @slot / @endcomponent
For inline component-like templates.
@component('components.alert') @slot('title') Error @endslot Something went wrong! @endcomponent
@props
Used in Blade component files to define props.
@props(['type' => 'info']) <div class="alert alert-{{ $type }}"> {{ $slot }} </div>
- Verbatim & Raw Output
@verbatim / @endverbatim
Prevent Blade from parsing.
@verbatim <script> let data = {{ $rawJson }}; </script> @endverbatim
{!! !!}
Unescaped output.
{!! $html !!}
- PHP and Debugging
@php / @endphp
Write raw PHP.
@php $status = 'active'; @endphp
@dd / @dump
Debug inside Blade.
@dd($user) @dump($posts)
- Switch Statement
@switch / @case / @break / @default / @endswitch
@switch($role) @case('admin') Admin Panel @break @case('editor') Editor Panel @break @default User Dashboard @endswitch
- Comments
Blade comment (not shown in HTML).
{{-- This is a Blade comment --}}
- Other Useful Directives
@csrf
Generates CSRF token input.
<form method="POST"> @csrf </form>
@method
Spoof HTTP method for forms (e.g., PUT, DELETE).
<form method="POST"> @method('PUT') </form>
@error / @enderror
Show validation errors.
@error('email') <div>{{ $message }}</div> @enderror
@json
<script> var users = {!! json_encode($users) !!}; </script> // translates to <script> var users = @json($users); </script>
Source: Orkhan Alishov's notes