Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
laravel
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:

@if(1=1)
{{ one will always be one.lol }}
@endif

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 }}
@endforech

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.
@endunless
  • The code snippet above does the opposite of the @if blade directives.

The isset and empty blade directives

@isset($records)

@endisset

@empty($records)
    
@endempty

  • The isset() and empty() blade directives work normally, as shown in the above code snippet.
@guest
    
@endguest
  • 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

@production
    
@endproduction
  • 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


@env('staging')
    // The application is running in "staging"...
@endenv

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

The hasSection blade directive

@hasSection('navigation')
    
@endif

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

The sectionMissing blade directive

 @sectionMissing('navigation')
    
@endif

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

The switch blade directive

@switch($i)
    @case(1)
        First case...
        @break
    @default
       
@endswitch

  • 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 }}
@endfor

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

RELATED TAGS

php
laravel
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring