Laravel Control Statements

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:

@if (count($records) === 1)
 One record;
 @elseif (count($records) > 1)
 Multiple records;
 No records;

Blade also provides an @unless directive:

 @unless (Auth::check())
 Not signed in.

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

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

Authentication Directives

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


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

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

 // The user is authenticated
 // The user is authenticated

Section Directives

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

 <div class="pull-right">
 <div class="clearfix"></div>

Switch Statements

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

 First case…
 Second case…
 Default case…


The Blade provides simple directives for working with loop structures.

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

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@foreach ($users as $user)
<p>User no. {{ $user->id }}</p>

@forelse ($users as $user)
<li>{{ $user->name }}</li>
<p>No User Found</p>

@while (true)
  • 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:

 @foreach ($users as $user)
 @continue($user->type == 1)
 <li>{{ $user->name }}</li>
 @break($user->number == 5)
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.

 @foreach ($users as $user)
     @if ($loop->first)
         First iteration.
     @if ($loop->last)
         Last iteration.
 <p> User no. {{ $user->id }}</p>

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

 @foreach ($users as $user)
     @foreach ($user->posts as $post)
         @if ($loop->parent->first)
          First iteration of the Parent Loop.

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 parent`s 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.

{{-- This comment will not be present in the rendered HTML --}}
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.