Loop variable laravel blade file use tips, while you iterate through the @foreach loop in the blade file, you may also use the $loop variable inside the @foreach property statement. This $loop variable will give you additional information about the loop, You may use this $loop variable with a different intent.
So in this post, we will take a little lesson on how the foreach loop directives statement is used in the blade file. we will also show different statements and uses.
The Loop Variable
While you iterate through a @foreach loop in the blade file, a $loop variable will be available inside of the @foreach loop. This variable provides you with some useful information such as the current loop index or a number of a loop whether the first time or last time looping iteration through the $loop variable.
@foreach ($users as $user) @if ($loop->first) This is the first iteration. @endif @if ($loop->last) This is the last iteration. @endif <p>This is user {{ $user->id }}</p> @endforeach
If you are using nested looping, you may access your parent loop using the $loop variable via the parent property.
@foreach ($users as $user) @foreach ($user->posts as $post) @if ($loop->parent->first) This is the first iteration of the parent loop. @endif @endforeach @endforeach
Where Clause In Laravel Eloquent Example
Some $loop variable useful contains a variety of properties :
Property | Description |
---|---|
$loop->index | The index of the current loop iteration (starts at 0). |
$loop->iteration | The current loop iteration (starts at 1). |
$loop->remaining | The iterations remaining in the loop. |
$loop->count | The total number of items in the array being iterated. |
$loop->first | Whether this is the first iteration through the loop. |
$loop->last | Whether this is the last iteration through the loop. |
$loop->even | Whether this is an even iteration through the loop. |
$loop->odd | Whether this is an odd iteration through the loop. |
$loop->depth | The nesting level of the current loop. |
$loop->parent | When in a nested loop, the parent's loop variable. |
Loop Statment Laravel
In this additional conditional loop statement in the laravel blade file. Laravel blade provides a simple directive for working with the PHP's core loop structure. Again, each of these directives functions identically to their PHP counterparts by looping a single time.
@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
Directive Declaration
You may skip the current iteration or end of the loop using the @continue statement and @break statement in the current loop. see the below example.
@foreach ($users as $user) @if ($user->type == 1) @continue @endif <li>{{ $user->name }}</li> @if ($user->number == 5) @break @endif @endforeach
You can also use directive declaration instead of @if condition in the foreach loop. This directive statement will identify the current loop.
@foreach ($users as $user) @continue($user->type == 1) <li>{{ $user->name }}</li> @break($user->number == 5) @endforeach
Sendgrid Email Send Tutorial Laravel/PHP/cURL Example
How To Filter In Laravel Query Example
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel 9 Dropzone Multiple File Upload Example
- Laravel Remove index.php From URL
- Laravel 6 Image Upload Tutorial
- All Guides About Soft Delete Laravel 8 And 9 With Examples
- HTTP Client Guzzle Package With Basic Usage In Laravel
- Laravel Carbon diffForHumans Arguments Passing
- Non-static method Illuminate\Http\Request::method() should not be called statically
- Laravel-9 Multi Authentication Guard Passport API Example
- How To Integrate Stripe Payment Gateway in Laravel 9
- Laravel pluck method example | Laravel Pluck