Laravel 9 Blade Template cheat sheet

Laravel 9 Blade Template cheat sheet | PDF Download

Laravel has a simple but powerful templating engine called Blade. You can read or download the Laravel 9 blade template cheat sheet for free in this article.

Unlike several PHP templating engines, Blade allows you to use plain PHP code in your templates. Blade template files are generally located in the resources/views directory and have the file extension .blade.php

Sending Data to Blade Views

There are several ways to send data to views:

return view('view-file', ['variable-name' => 'value']);
return view('view-file')
            ->with('variable1', 'value')
            ->with('variable2', 'value');

You can pass an array of variables using the PHP compact() method

return view('view-file', compact(['var1','var2',....'varN']));

Displaying Data

The contents of the variables can be displayed as follows:

{{ $variable }}

Any PHP function’s output can also be displayed. In fact, you may include any PHP code you want within a Blade echo statement:

{{ time() }}

Blade and JavaScript Frameworks’ syntax conflict

Some JavaScript frameworks also use “curly” braces to display data, you may use the @ symbol to tell the Blade rendering engine that an expression should be ignored. As an example:

Hello, @{{ name }}

Blade directives can also be escaped using the @ symbol:

{{-- Blade template --}}


If you’re displaying many JavaScript variables in your template, you can use the @verbatim directive to avoid having to preface each Blade echo statement with a @ symbol:

    <div class="container">
        Hello, {{ name }}.

Rendering JSON

The most recent versions of the Laravel application offer a Js facade, which allows you to easily render it as JSON in order to initialize a JavaScript variable:

    var app = {{ Js::from($array) }};

Syntax of If Statements

@if (count($user) === 1)
    single user
@elseif (count($user) > 1)
    many users!
    no user found!

Conditional statement – unless

@unless (Auth::check())
    You are not signed in.

Conditional statements – isset and empty

    // $records is defined and is not null...
    // $records is "empty"...

Authentication Directives

    // The user is authenticated...
    // The user is not authenticated...

You can provide the authentication guard that should be verified if necessary:

    // The user is authenticated...
    // The user is not authenticated...

Environment Directives

    // application is running in a production
    // application is running in staging
@env(['staging', 'production'])
    // application is running in staging or production

Switch statement

        First case...
        Second case...
        Default case...

Loops in blade

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@forelse ($users as $user)
    <li>{{ $user->name }}</li>
    <p>No users</p>
@while (true)
    <p>I am looping forever.</p>

The Loop Variable

@foreach ($users as $user)
    @if ($loop->first)
        This is the first iteration.
    @if ($loop->last)
        This is the last iteration.
    <p>This is user {{ $user->id }}</p>

Conditional Classes

    $isActive = false;
    $hasError = true;
<span @class([
    'font-bold' => $isActive,
    'text-gray-500' => ! $isActive,
    'bg-red' => $hasError,
<span class="p-4 text-gray-500 bg-red"></span>

Including Subviews


you can pass an array of data

@include('', ['status' => 'complete'])

If you want to include a view that might or might not be present, use the @includeIf directive:

@includeIf('', ['status' => 'complete'])

If you want to @include a view based on whether a specified boolean expression evaluates to true or false, you can use the @includeWhen and @includeIf

@includeWhen($boolean, '', ['status' => 'complete'])
@includeUnless($boolean, '', ['status' => 'complete'])

You may use the @includeFirst directive to include the first view in a supplied array of views:

@includeFirst(['custom.admin', 'admin'], ['status' => 'complete'])

The @each directive

@each('', $jobs, 'job')
@each('', $jobs, 'job', 'view.empty')

The @once Directive

The @once directive specifies a section of the template that will be evaluated only once per rendering cycle.

// This code will render once

This is suitable to push some javascript code:

            // Your JavaScript code

You can also use @pushOnce

        // Your custom JavaScript...

Writing Raw PHP

    $var = "some value";

Comments in Blade Template

{{-- This is an example of a comment in Blade template --}}

Download the Laravel Blade Template cheat sheet


Laravel 5.1 introduces the concept of using Blade, a templating engine. The blade is distinct from other templating engines in the following ways:

  • It does not preclude the developer from writing simple PHP code in views.
  • The blade views created in this manner are compiled and cached until they are updated.

Add comment