# Blade Directives
# If Statements
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
For convenience, Blade also provides an @unless
directive:
@unless (Auth::check())
You are not signed in.
@endunless
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
# Authentication Directives
The @auth
and @guest
directives may be used to quickly determine if the current user is authenticated (opens new window) or is a guest:
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
If needed, you may specify 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 not authenticated...
@endguest
# Environment Directives
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
# Section Directives
You may determine if a template inheritance section has content using the @hasSection
directive:
@hasSection('navigation')
<div class="pull-right">
@yield('navigation')
</div>
<div class="clearfix"></div>
@endif
You may use the sectionMissing
directive to determine if a section does not have content:
@sectionMissing('navigation')
<div class="pull-right">
@include('default-navigation')
</div>
@endif
# Session Directives
The @session
directive may be used to determine if a session (opens new window) value exists. If the session value exists, the template contents within the @session
and @endsession
directives will be evaluated. Within the @session
directive's contents, you may echo the $value
variable to display the session value:
@session('status')
<div class="p-4 bg-green-100">
{{ $value }}
</div>
@endsession
# Switch Statements
Switch statements can be constructed 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
In addition to conditional statements, Blade provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
@while (true)
<p>I'm looping forever.</p>
@endwhile
# The @once Directive
The @once
directive allows you to define a portion of the template that will only be evaluated once per rendering cycle. This may be useful for pushing a given piece of JavaScript into the page's header using stacks (opens new window). For example, if you are rendering a given component (opens new window) within a loop, you may wish to only push the JavaScript to the header the first time the component is rendered:
@once
@push('scripts')
<script>
// Your custom JavaScript...
</script>
@endpush
@endonce
Since the @once
directive is often used in conjunction with the @push
or @prepend
directives, the @pushOnce
and @prependOnce
directives are available for your convenience:
@pushOnce('scripts')
<script>
// Your custom JavaScript...
</script>
@endPushOnce
← Views Components →