Laravel 9 Foreach Loop Variable Example

Websolutionstuff | Jul-22-2022 | Categories : Laravel

In this article, we will see laravel 9 foreach loop variable example. Laravel provides a simple blade template that provides many directives to use directly in HTML code. Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. Laravel blade has a foreach directive.

@foreach directive is the same as foreach loop. While iterating through a foreach loop, you may use the loop variable to gain valuable information about the loop. The $loop variable is used to get information about the first or last iteration through the loop.

So, let's see foreach loop variable laravel 9, foreach loop in laravel 9 blade, laravel 9 foreach loop in the controller.

@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 in a nested loop, you may access the parent loop's $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

 

 

The index of the current loop iteration (starts at 0).

@php
$numbers = [1, 3, 5, 7, 9, 11];
@endphp

@foreach ($numbers as $number)
    @if ($loop->index == 2)
        {{ $number }} // 5
    @endif
@endforeach

 

The current loop iteration (starts at 1).

@php
$numbers = [1, 3, 5, 7, 9, 11];
@endphp

@foreach ($numbers as $number)
    @if ($loop->iteration == 3)
        {{ $number }} // 5
    @endif
@endforeach

 

The $loop variable also contains a variety of other useful 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.

 


You might also like:

Recommended Post
Featured Post
How to Use Laravel Factory in Seeder
How to Use Laravel Factory in...

Laravel is a popular PHP framework known for its elegance and simplicity in building web applications. When it comes to...

Read More

Sep-13-2023

Laravel orderBy, groupBy and limit Example
Laravel orderBy, groupBy and l...

In this article, we will see the laravel orderBy, groupBy, and limit examples. Here we will see different types of&...

Read More

Jan-27-2021

How To Redirect Another Page Using Javascript
How To Redirect Another Page U...

In this article, we will see how to redirect another page using javascript. we will redirect the page to another pa...

Read More

Nov-04-2022

How To Send Email using Node.js
How To Send Email using Node.j...

Hello Friends, In this tutorial we will learn how to send email using node.js app. In this tutorial we will see send...

Read More

Jul-28-2021