Laravel Blade Foreach Loop Example

BOOKC, this time share about Laravel Blade Foreach Loop 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 and laravel 9.

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

Thanks for visit my blog. :)

Bookc
Admin

Hopefully the information we provide can be useful.

Press ESC to close