Control flow
Control flow decides what a view renders. It uses the same directives as Blade: @if, @foreach and @forelse, each closed by its own end directive.
@if, @elseif and @else
The first true branch renders, and the rest are skipped. The condition is a field of the context. A value is false when it is null, false, zero, an empty string, or an empty list. A struct is true.
@if ($is_admin)
<p>Admin</p>
@elseif ($is_member)
<p>Member</p>
@else
<p>Guest</p>
@endif
Live example
Member
@foreach
Repeats the body for each item. Inside the loop, the loop variable (here tag) holds the current item.
@foreach (tag in tags)
<li>{{ $tag }}</li>
@endforeach
Live example
- rust
- rocket
- propel
@forelse and @empty
Like @foreach, but it renders the @empty branch when the list has no items. A null list counts as empty.
@forelse (item in items)
<li>{{ $item }}</li>
@empty
<li>Nothing here yet.</li>
@endforelse
Live example, with an empty list
- Nothing here yet.
Optional values
Reading a field of a null value is an error, as in Blade. Check an optional value with @if first. A null value at the end of a path, such as $author on its own, prints nothing.
@if ($author)
<p>Author: {{ $author.name }}</p>
@else
<p>No author yet.</p>
@endif
Live example, with no author
No author yet.
Propel