Laravel Blade is one of the most powerful and advanced templating engine as compared to other templating engines such as smarty and twig.


@include

Allows you to include another view file into your view. When we use this directive all data variables available to parent view are also available to target view. You can use this directive as below:

@include('partials.sidebar')

You can also manually pass an array as a second parameter to send the data to target view like below:

@include('partials.sidebar', ['menu' => $menu])

@php

As I said earlier, you can use PHP code inside your Laravel Blade views, all you have to do is adding an opening tag of PHP and then closing tag of PHP. Luckily, Laravel provides a blade directive which is easy to use and looks a lot cleaner in your blade views.

You can use @php and @endphp directives to execute plain PHP code in your blade views like below:

@php
    // you php code here
@endphp

@each

Blade's @each directive allows you to combine loop and include into one line. For example:

@each('users.index', $users, 'user')

The first argument passed to this directive is the view you want to render for each element of $user. The second argument is the variable you want to iterate through, in our case it's $users. The third argument is the variable you want to assign for the iteration inside this directive, in our case it's user.

You can also pass the fourth argument which will determine the view that will be rendered if the given object or array is empty.

@each('users.index', $users, 'user', 'users.not_found')

@includeWhen

The @includeWhen is the extension of @include directive. This directive will include a view if the given condition is true.

@includeWhen($isUserAdmin, 'users.admin_card', ['user' => $user])

In our above example, @includeWhen() directive will check the value of $isUserAdmin variable and if it's true, it will include the admin user card view while passing the current $user to admin_card view.


@forelse

Consider a scenario, where you have to print a list of all user's names. For this, you would have to run a loop, but you have to make a check if the user exists then run the loop. Something like below:

@if($users->count() > 0)
    @foreach($users as $user)
        {{ $user->name }}
    @endforeach
@else
    No Users Found	
@endif

You can simplify the above code by using Laravel Blade's @forelse directive. So using @forelse above code will translate to:

@forelse($users as $user)
    {{ $user->name }}
@empty
    No Users Found
@endforelse

Much simple and cleaner.


@isset and @empty

Normally in PHP code we use isset() and empty() functions to check if the value of a variable is set or not. Laravel Blade provides @isset() and @empty() directive to replace PHP code with nice directive.

@if(isset($users))
    // your logic here
@endif

@if(empty($users))
    // your logic here
@endif

Above code will translate to the respective blade directives like below:

@isset($users)
    // your logic here
@endisset

@empty($users)
    // your logic here
@endempty

@csrf and @method

When creating HTML forms in blade views, you have to specify a hidden field containing a csrf token to use the CSRF protection. You can use the @csrf directive which will render a hidden input with a csrf token in it.

<form method="POST" action="/profile">
    @csrf
    ...
</form>

Since you can not make a PUT, PATCH and DELETE request using an HTML form as it's not supported. You will need to add a hidden _method field to spoof these HTTP verbs.

The @method directive can create this field for you like below:

<form action="/api/call" method="POST">
    @method('PUT')
    ...
</form>

@auth and @guest

When checking if the user is authenticated, you could check if the user isn't null:

@if(auth()->user())
    // The user is authenticated.
@endif

However, Laravel ships with a custom Blade directive that provides the same functionality more cleanly:

@auth
    // The user is authenticated.
@endauth

The inverse of the authentication, we can check if the user is not authenticated using the guest() method on the auth helper:

@if(auth()->guest())
    // The user is not authenticated.
@endif

But Laravel also provides a @guest directive:

@guest
    // The user is not authenticated.
@endguest

We can also combine those two directives using the else statement:

@guest
    // The user is not authenticated.
@else
    // The user is authenticated.
@endguest

@continue and @break

Blade has the continue and break directives that make it possible to continue and break inside a loop.

@foreach ($blogs as $blog)
    @if (!$blog->is_validated)
        @continue
    @endif

    <li>{{ $blog->title }}</li>

    @if ($blog->is_last)
        @break
    @endif
@endforeach

We can optimize the example above by including the condition in the continue and break directive, so that it could be refactored into one line of code:

@foreach ($blogs as $blog)
    @continue(!$blog->is_validated)

    <li>{{ $blog->title }}</li>

    @break($blog->is_last)
@endforeach

This looks much cleaner, right?


@stack and @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>

@includeFirst

Building a website with multiple themes might require including a file if it exists or including another one if it doesn't, you can easily achieve this with simple blade conditions:

@if(view()->exists('first-view-name'))
    @include('first-view-name')
@else
    @include('second-view-name')
@endif

There is a shorter and a much cleaner directive for including the first found template:

@includeFirst(['first-view-name', 'second-view-name']);

@includeIf

If you have custom themes system or you dynamically create your Blade views, then checking if the file exists a mandatory thing to do.

Calling the exists method on the view helper will do the trick:

@if(view()->exists('view-name'))
    @include('view-name')
@endif

But it turns out there is a simple way of handling this using the includeIf Blade directive:

@includeIf('view-name')

@json

<script>
    var users = {!! json_encode($users) !!};
</script>

// translates to

<script>
    var users = @json($users);
</script>

@hasSection

Checks if a section is present in the child view.

@hasSection('navigation')
    <div class="nav">
        @yield('navigation')
    </div>
@endif