Convert Array to Custom Comma Separated List

If you build a Laravel app, you sometimes may want to convert an array to a custom comma separated list, for example when showing tags.

A solution for that problem might be the following Blade code:

@foreach ($note->tags as $tag)
    {{ $loop->first ? '' : ', ' }}
    <a href="{{ route( 'tags.show', $tag) }}">{{ $tag->name}}</a>
    {{ $loop->last ? ' | ' : '' }}
@endforeach

The $loop variable was introduced in PHP 5.3 and shows you, if you are in the first or the last iteration. That’s helpful for adding commas and a final char to show the end of the comma separated list (if desired).