Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

community creator

What are blade directives?

Chinweuba Elijah Azubuike

What are blade directives?

Blade directives are shortcut codes for the implementation of basic PHP structure control, such as loop and conditional statements. It makes your code snippets clean and easy to understand.

Note: These directives are basically only used in the blade template, so don’t try to use them on your controllers.

In this shot, we look at some important blade directives.

Please refer to this shot if you don’t know the basics of blade templating.

The if blade directive

  • @if()
  • @elseif()
  • @else
  • @endif

The if(){} statement is similar to the endif statement, except that the endif statement will serve as the closing {} curly braces.

The if(){} statement can be used like so:

{{ one will always be }}

The Auth blade directive

  • @auth
  • @endauth

This blade directive is used to check if a particular user has been authenticated.

Refer to this shot for more information on this.

The foreach blade directive

  • @foreach()
  • @endforech

These are loop directives and they work like the normal foreach(){}, except this directive is cleaner.

The foreach blade directive can be used like so:

@foreach($users as $user)
{{ $user->name }}

The csrf blade directive

  • The @csrf directive is used in the form.
  • It is also used in Laravel applications to verify tokens.

Please refer to this shot to learn more on csrf protection.

  • {{ }} this is the most used blade directive and is similar to the <?php echo $string; ?>. It is basically used to echo the values of variables. It is used like so: {{ date('Y') }}.

The unless blade directive

@unless (Auth::check())
    You are not signed in.
  • The code snippet above does the opposite of the @if blade directives.

The isset and empty blade directives




  • The isset() and empty() blade directives work normally, as shown in the above code snippet.
  • The above code snippet checks if the the current user is a guest, i.e., if the user is not authenticated.

The production blade directive

  • There are times when you want to run a particular code while your application is on production. In the above example, your code will only execute if the application is in production.

The env blade directive

    // The application is running in "staging"...

@env(['staging', 'production'])
  • You can choose what environment you want to use to execute any code with the above code snippet.

The hasSection blade directive


  • The code above checks if a section inherited has content.

The sectionMissing blade directive


  • The above code checks if the section’s contents are missing.

The switch blade directive

        First case...

  • Just like your normal switch statement, the @switch() blade directive functions the same.

The for loop blade directive

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}

The blade directives are just simple and short forms that Laravel uses to simplify some PHP functions to make your codes simple and readable.


community creator


Chinweuba Elijah Azubuike

View all Courses

Keep Exploring