Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 844
Ariando Miller Avatar asked Dec 23 '22 23:12

Ariando Miller


2 Answers

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.

like image 186
Alexey Mezenin Avatar answered Dec 28 '22 07:12

Alexey Mezenin


Try this

   @foreach($journal->user as $item)
        {{ $item->name }}
        @if (!$loop->last)
        ,
        @endif
   @endforeach
like image 25
Rimon Khan Avatar answered Dec 28 '22 06:12

Rimon Khan