Blade also provides convenient shortcuts for common control structure in PHP, such as conditional statements and loops.

These shortcuts provide a very clean working with PHP structures, while also remain familiar to its PHP counterparts.

If Statements        

We construct if statements using the @if, @elseif, @else, and @endif directives.

These directives function are identical to its PHP counterparts:

Blade also provides an @unless directive:

The @isset and @empty directives are used as the convenient shortcuts for its PHP functions:

Authentication Directives

The @auth and @guest directives are used to determine the current user authentication for the guest:


We define the authentication guard that should be checked when using the @auth and @guest directives:

Section Directives

We need to check if a section has content using the @hasSection directive:

Switch Statements

Switch statements are constructed by using the @switch, @case, @break, @default, and @endswitch directives:


The Blade provides simple directives for working with loop structures.

Each of these directives functions is identical to its PHP counterparts.

  • Use the loop variable to gain the information about the loop, like whether, we are in the last or first iteration through the loop.

We include the condition with the directive declaration in one line:

Switch Statements 2

The Loop Variable

A $loop variable will be available inside our loop. The variable provides access to useful bits of information like the current loop index, and whether it is the first or last iteration through the loop.

If nested loop, we access the parent loops $loop variable with the parent property:

The $loop variable also contains different types of other useful properties:

Property Description
$loop->index The current loop iteration of the index. (starts at 0).
$loop->iteration The current loop iteration starts from 1.
$loop->remaining The iterations remain in the loop.
$loop->count The array being iterated by the total number of items.
$loop->first Whether it is the first iteration through the loop.
$loop->last Whether it is the last iteration through the loop.
$loop->even Whether it is an even iteration through the loop.
$loop->odd Whether, it is an odd iteration through the loop.
$loop->depth The nesting level of the current loop.
$loop->parent The parents loop variable, when it is a nested loop.

Blade also allows us to define comments in our views. Blade comments are not included in the HTML returned by our application.

Switch Statements 5

It is useful to embed PHP code into our views. We can use the Blade @php directive for executing a block of plain PHP code within our template.

Blade provides this feature. By frequently using it may signal us that we have much logic embedded within our template.

Pin It on Pinterest

Share This