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 Validate Max File Size Using Javascript
How To Validate Max File Size...

This article will show us how to validate max file size using javascript. Many times we have a requirement to check...

Read More

Aug-03-2020

Laravel 8 PDF Generate Example
Laravel 8 PDF Generate Example

In this article, we will see a laravel 8 pdf generate example. For generating PDF file we will use the laravel-dompdf pa...

Read More

Oct-17-2020

How to Create New Project in Angular 17 Example
How to Create New Project in A...

As a beginner, diving into a new framework like Angular 17 can seem daunting, but fear not! I'll guide you through c...

Read More

Mar-20-2024

Laravel 8 Left Join Query Example
Laravel 8 Left Join Query Exam...

In this tutorial I will give you laravel 8 left join query example. laravel left join eloquent returns all rows from the...

Read More

Nov-26-2021