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 How to Add Google reCAPTCHA in Laravel 9 8 7 6 5

How to Add Google reCAPTCHA in Laravel 9 / 8 / 7 / 6 /5 ?

March 11, 2022March 11, 2022

Google reCAPTCHA used widely in may websites, in laravel its easy to use with third party package. Captcha is used to enhance the security of form. By adding the Captcha in laravel form we can prevent attackers to submit the form using the automated scripts and it adds an extra…

Read More
Php Laravel sum in eloquent query

Laravel sum in eloquent query with example

January 23, 2022November 10, 2023

In this article we will learn to use aggregate function sum in laravel eloquent and query builder. Laravel itself provide inbuilt method to sum the columns like in MySQL or SQL based database we can do sum of columns using sum aggregate function. Sum method will give you the sum…

Read More

What is Access Modifiers ?

July 18, 2021August 2, 2021

In object oriented programming(oops) to set the visibility of classes, variables and methods we are using access modifiers. So Access modifiers are used the encapsulation of class properties. There are three types of access modifiers in any language (Php, java or c etc. ) Private Protected Public 1. Private Access…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version