How to use Foreach Loop in Laravel Blade View (Laravel 5, 6, 7 and 8 Example)

A foreach statement allows us to loop through an array of data and do something with each element inside it. In the Laravel framework, blade view files use a special syntax to call regular PHP functions such as foreach, which is what we will be learning to use in this tutorial.

 

To run a foreach loop in a Laravel blade, use the following:

 

@foreach ($items as $k => $v)
   
  {{ $v->id }}

@endforeach

 

The biggest difference between the Laravel syntax and the syntax of a plain PHP foreach is that it must be ended using an @endforeach statement. The advantage is you get really clean code without having to open and close PHP throughout your view file.