Create comma separated list from array in laravel/blade?
Use this. We can implement it using $loop->last
@foreach ($arrayOrCollection as $value)
<span class="nice">
{{ $value->first_name }}
@if( !$loop->last)
,
@endif
</span>
@endforeach
Use implode:
{{ implode(', ', $tags) }}
implode()
is good for echoing simple data. In real project you usually want to add some HTML or logic into the loop, use $loop
variable which is available since 5.3:
@foreach ($arrayOrCollection as $value)
{{ $loop->first ? '' : ', ' }}
<span class="nice">{{ $value->first_name }}</span>
@endforeach
The selected answer is too complicated. Laravel has a simpler solution:
{{ $items->pluck('tag')->implode(', ') }}