Preston Garvey
Preston Garvey

Reputation: 1421

How to remove comma from last array in Laravel 5?

I have this view :

<p style="text-align: center;"><strong>
@foreach($journal->user as $item)
        {{ $item->name }},
@endforeach
</strong></p>

I wanted to remove comma after the last {{ $item->name }} string. How to do it in Laravel 5.3 blade ?

Upvotes: 5

Views: 2680

Answers (2)

Try this

   @foreach($journal->user as $item)
        {{ $item->name }}
        @if (!$loop->last)
        ,
        @endif
   @endforeach

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

If you're using 5.3, you can use $loop variable for this:

@foreach($journal->user as $item)
    {{ $loop->first ? '' : ', ' }}
    {{ $item->name }}
@endforeach

The code is from similar question.

Upvotes: 9

Related Questions