Looping in Laravel blade template

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>

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

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

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

No comments :

Post a Comment

Leave A Comment...