Skip to main content

Directive

conditional directives

@if
@if($post['is_new'])
<h5>This is a new one!</h5>
@else
<h5>This is an old one!</h5>
@endif
@unless :condition has to be false
@unless($post['is_new'])
<h5>This is an old one!</h5>
@endunless

loop directives

  • @for
@for($i=0;$i<10;$i++)
<li>The current value is {{$i}}</li>
@endfor
  • @foreach
@foreach($users as $user)
<li>This is user {{$user['id']}}</li>
@endforeach
  • @forelse :render only when array is not empty
@forelse ($users as $user)
This is user {{ $user->id }}
@empty
No users found.
@endforelse
  • @while
@php $count=0 @endphp
@while($count<10)
<li>The current count is {{$count}}</li>
@php $count++ @endphp
@endwhile

@isset directive

  • checks whether a variable is set, which means that it has to be declared and is not NULL
@isset($post['has_comment'])
<div>Here are the comments...</div>
@isset

@include directive

  • allows any view to include any other subviews
@include('subview')
@include('subview',['data' => '123']) //can pass some data to the subview
@include('subview',['posts' => $posts]) //can pass array to the subview