How to loop through an array and display items in a Blade template?

To loop through an array and display its items in a Blade template, you can use the @foreach directive provided by Laravel Blade. Here's an example of how you can do it:

  1. First, pass the array from your controller to the Blade template in your Laravel application.
  2. In your Blade template, use the @foreach directive to loop through the array and display its items.

Here's an example:

  1. Pass the array to the Blade template:
// Controller $array = ['item1', 'item2', 'item3']; return view('my-template', ['array' => $array]);
  1. Loop through the array and display its items in the Blade template:
<!-- Blade Template: my-template.blade.php --> @foreach($array as $item) <p>{{ $item }}</p> @endforeach

In the above code, the @foreach directive loops through each item in the $array variable. For each item, a <p> element is generated that contains the value of the item using the {{ $item }} syntax.

This will result in the following output when the Blade template is rendered:

<p>item1</p> <p>item2</p> <p>item3</p>

Note: Make sure to adjust the array variable and template name according to your specific application and template files.