Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel blade for and loop varible

Laravel blade foreach, for and loop variable with example

Aman Jain, December 18, 2021October 4, 2022

Laravel blade provides easy way to iterate a long list array or loop using @foreach and @for. @foreach and @for both used same structure as php foreach and for. In this article i will show you to use @foreach and @for loop with example and also the $loop variable. Loop variable is always available inside the loop and we can access it using $loop object.

Here is the syntax for for loop

@for ($count = 0; $count < 10; $count++)
    The Counter value is {{ $i }}
@endfor

and syntax for foreach loop

@foreach ($posts as $post)
    <p>Title :  {{ $post->title }}</p>
@endforeach

as we can see syntax is equivalent to php for loop, forelse is also can be used if array is empty then we can show else block as below

@forelse ($posts as $post)
    <li>{{ $post->title }}</li>
@empty
    <p>No posts</p>
@endforelse

Using loop variable inside foreach

$loop variable is used inside the foreach loop to access the some useful information like first, last, count, index etc. Below is the list of use of variables.

$loop->indexThe index of the current loop iteration (starts at 0).
$loop->iterationThe current loop iteration (starts at 1).
$loop->remainingThe iterations remaining in the loop.
$loop->countThe total number of items in the array being iterated.
$loop->firstWhether this is the first iteration through the loop.
$loop->lastWhether this is the last iteration through the loop.
$loop->evenWhether this is an even iteration through the loop.
$loop->oddWhether this is an odd iteration through the loop.
$loop->depthThe nesting level of the current loop.
$loop->parentWhen in a nested loop, the parent’s loop variable.

Source of table from laravel

Example of $loop variable

@foreach ($posts as $post)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>loop index {{ $loop->index}} </p>
@endforeach

Let’s understand Laravel blade foreach, for and loop variable with example

Step 1: Create a fresh laravel project

composer create-project --prefer-dist laravel/laravel blog

Step 2: Add routes in routes/web.php

Create routes to call our controller and blade file in it.

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
 
Route::get('/example',function(){
 
 $posts=[["title"=>"test","id"=>"1"],["title"=>"test2","id"=>"2"]];
 return view("example-view",["posts"=>$posts]);
});

Step 3: Create View File

Now, create a example-view.blade.php in resource/views folder.

@foreach ($posts as $post)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>loop index {{ $loop->index}} </p>
 <p>Title : {{ $post['title']}} </p>
@endforeach

Related

Php Laravel for looplaravelphp

Post navigation

Previous post
Next post

Related Posts

Php Image Validation in laravel 8

How to use image validation in Laravel 8 with example

November 21, 2021January 26, 2022

Laravel provides multiple ways to validate a form or a image in form with it’s own validation library. In this tutorial we will learn about the image validation for specific extension and file size. In this tutorial i will use a simple form and a image file input field to…

Read More

Laravel OTP Login and Registration Without Password

September 1, 2022March 16, 2024

Today most of the applications are using laravel otp login and registration without password for ease of login and registration and also provides easy to login without remembering the password each time while login or register. In Laravel there is multiple ways to implement the login and registration like Laravel…

Read More
Php How to Send Mail in Laravel Through Sendmail and SMTP

How to Send Mail in Laravel 8 / 9 Through Sendmail and SMTP ?

May 8, 2022August 20, 2022

Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. In this tutorial i will show you to send Mail in Laravel Through Sendmail and SMTP. Laravel provides multiple drivers or services to send mail from different…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version