Looping in Laravel blade template - BOOKC -->

Looping in Laravel blade template

Saturday, March 25, 2017


Just like the 'if' statement, blade also has it's own way of doing loops/looping, you can do 'for' loops, 'foreach', 'forelse', and also 'while' loops. Let's take a look at the syntax.

'for' loops syntax
@for ($i = 0; $i < 10; $i++)
<p>The current value is {{ $i }}</p>
@endfor

'foreach' loops syntax
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach

'forelse' loops syntax
@forelse($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse

'while' loops syntax
@while (true)
<p>I'm looping forever.</p>
@endwhile