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

Javascript Laravel Ajax Autocomplete Using Select2

Laravel Customized Autocomplete Options Using Select2

July 18, 2022July 18, 2022

In this article we will learn to use Laravel Customized Autocomplete Options Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and…

Read More
Php How to Fetch Records Between Two Date Range in Laravel

How to Fetch Records Between Two Date Range in Laravel ?

September 6, 2022March 16, 2024

Many times we want to fetch records between two date range in laravel like find all records which is created between start yesterday and end today so in that case we need to check it with created_at columns in database but created_at column also has time and if we directly…

Read More
Php Laravel slug generator

Laravel slug generator with example

November 8, 2021November 8, 2021

In this article we are going to create slug from any input field. slug is useful search engine optimization and also from security perspective because in some case we use primary id or unique id of table to fetch the detail in page but its not recommended way to show…

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