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; @else No records; @endif
Blade also provides an @unless directive:
@unless (Auth::check()) Not signed in. @endunless
The @isset and @empty directives are used as the convenient shortcuts for its PHP functions:
@isset($records) // $records is defined and is not null... @endisset @empty($records) // $records is "empty"... @endempty
Authentication Directives
The @auth and @guest directives are used to determine the current user authentication for the guest:
Syntax:
@auth // The user is authenticated @endauth @guest // The user is not authenticated @endguest
We define the authentication guard that should be checked when using the @auth and @guest directives:
@auth(‘admin’) // The user is authenticated @endauth @guest(‘admin’) // The user is authenticated @endguest
Section Directives
We need to check if a section has content using the @hasSection directive:
@
hasSection('navigation') <div class="pull-right">@
yield('navigation') </div> <div class="clearfix"></div>@
endif
Switch Statements
Switch statements are constructed by using the @switch, @case, @break, @default, and @endswitch directives:
@switch($i) @case(1) First case… @break @case(2) Second case… @break @default Default case… @endswitch
Loops
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 }} @endfor @foreach ($users as $user) <p>User no. {{ $user->id }}</p> @endforeach @forelse ($users as $user) <li>{{ $user->name }}</li> @empty <p>No User Found</p> @endforelse @while (true) <p>Hello Looping.</p> @endwhile
- 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) @endforeach
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. @endif @if ($loop->last) Last iteration. @endif <p> User no. {{ $user->id }}</p> @endforeach
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. @endif @endforeach @endforeach
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 --}}
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.
@php // @endphp
Blade provides this feature. By frequently using it may signal us that we have much logic embedded within our template.