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:
@foreach
directive to loop through the array and display its items.Here's an example:
// Controller
$array = ['item1', 'item2', 'item3'];
return view('my-template', ['array' => $array]);
<!-- 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.