ItSolutionStuff.com

Laravel Blade Foreach Loop Example

By Hardik Savani • April 16, 2024
Laravel

Hello Dev,

This post is focused on laravel blade foreach example. you will learn laravel foreach loop in blade. step by step explain laravel foreach in blade. we will help you to give example of laravel foreach loop example. follow bellow step for foreach laravel blade.

simple example of foreach loop statement in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

let's see bellow simple example:

Blade File Code:

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<ul>

@foreach ($users as $user)

<li>ID: {{ $user['id'] }}, NAME: {{ $user['name'] }}</li>

@endforeach

</ul>

</body>

</html>

Controller File Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AjaxController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function index()

{

$users = [

[ 'id' => 1, 'name' => 'Hardik'],

[ 'id' => 2, 'name' => 'Vimal'],

[ 'id' => 3, 'name' => 'Harshad'],

];

return view('ajaxRequest', compact('users'));

}

}

Output:

ID: 1, NAME: Hardik

ID: 2, NAME: Vimal

ID: 3, NAME: Harshad

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Blade If Multiple Conditions Example

Read Now →

Laravel Blade If Condition Example

Read Now →

Laravel nl2br Blade Directive Example

Read Now →

How to Create Custom Blade Directive in Laravel?

Read Now →

Laravel Blade Check If Variable is Set or Not Example

Read Now →

How to Write PHP Code in Laravel Blade?

Read Now →

Laravel - How to Check If Array is Empty in Blade?

Read Now →

Laravel - How to Get .env Variable in Blade or Controller?

Read Now →

Laravel 5.5 New Feature - BladeIf Directive Example

Read Now →

Laravel Ajax Render View With Data Example

Read Now →