Important Blade Directives Laravel
The if
blade directive
@if()
@elseif()
@else
@endif
The
@
symbol may also be used to escape Blade directives:
{{-- Blade template --}}@@if() <!-- HTML output -->@if()
You may construct
if
statements using the@if
,@elseif
,@else
, and@endif
directives. These directives function identically to their PHP counterparts:
@if (count($records) === 1) I have one record!@elseif (count($records) > 1) I have multiple records!@else I don't have any records!@endif
The foreach
blade directive
@foreach()
@endforech
While iterating through a
foreach
loop, a$loop
variable will be available inside of your loop. This variable provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop:
@foreach ($users as $user) @if ($loop->first) This is the first iteration. @endif @if ($loop->last) This is the last iteration. @endif <p>This is user {{ $user->id }}</p>@endforeach
The csrf
blade directive
- The
@csrf
directive is used in theform
. - It is also used in Laravel applications to verify tokens.
Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the
@csrf
Blade directive to generate the token field:
<form method=”POST” action=”/profile”> @csrf…</form>
The unless
blade directive
For convenience, Blade also provides an
@unless
directive:
@unless (Auth::check())You are not signed in.@endunless
The isset
and empty
blade directives
In addition to the conditional directives already discussed, the
@isset
and@empty
directives may be used as convenient shortcuts for their respective PHP functions:
@isset($records)// $records is defined and is not null…@endisset@empty($records)// $records is “empty”…@endempty
The production
blade directive
You may check if the application is running in the production environment using the
@production
directive:
@production// Production specific content…@endproduction
Or, you may determine if the application is running in a specific environment using the
@env
directive:
@env('staging')// The application is running in "staging"...@endenv@env(['staging', 'production'])// The application is running in "staging" or "production"...@endenv
The verbatim
directive
If you are displaying JavaScript variables in a large portion of your template, you may wrap the HTML in the
@verbatim
directive so that you do not have to prefix each Blade echo statement with an@
symbol:
@verbatim<div class="container">Hello, {{ name }}.</div>@endverbatim
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.