Laravel Blade Foreach Loop Example

In this example i focused on laravel blade foreach loop example. We will show how to use foreach loop in laravel blade. In this example you will learn laravel foreach loop in blade. You will teach laravel foreach loop use in blade example.

In this example i explain step by step laravel foreach in blade. we will help you to give example of laravel foreach loop example. The foreach loop through a block of code for each element in an array.

Read Also :  Laravel Insert Record using jQuery Ajax

We will show simple example of foreach loop statement in laravel 6, laravel 7 and laravel 8.

Step 1: Create route

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('', [UserController::class,'index']);

Step 2: Create controller

In this step we will create UserController for use laravel blade foreach loop. So let’s following code:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
  public function index()
  {
    $users = User::all();
    return view('users', compact('users'));
  }

}

Step 3: Create view

In this step we create users.blade.php file for write laravel blade foreach loop code.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Blade Foreach Loop Example - web-tuts.com</title>
</head>
<body>
    @foreach ($users as $user)
      {{ $user->email }} <br>
    @endforeach
</body>
</html>

I hope you understand of laravel blade foreach loop example and it can help you…