Suppose the array has ["Volvo", "BMW", "Toyota", "fbf", "fddg",.....] The how can we display the above array like below ?
<div>
<span>Volvo</span>
<span>BMW</span>
</div>
<div>
<span>Toyota</span>
<span>fbf</span>
</div>
and so on in laravel/php?
CodePudding user response:
maybe this code will help you..
@php
$array = ["Volvo", "BMW", "Toyota", "fbf", "fddg"];
$count = 1;
@endphp
@foreach ($array ?? [] as $value )
@if($count != 2)
<div>
<span>{{$value}}</span>
@elseif($count == 2)
<span>{{$value }}</span>
</div>
@php
$count=0;
@endphp
@else
</div>
@endif
@php
$count ;
@endphp
@endforeach
CodePudding user response:
Laravel provides a $loop
variable inside the @foreach
loop
You can use it like this:
@foreach ($collection as $item)
@if ($loop->iteration % 2)
<div>
<span>{{ $item }}</span>
@else
<span>{{ $item }}</span>
</div>
@endif
@if ($loop->count % 2 && $loop->last)
</div>
@endif
@endforeach