Laravel blade - Remove the last comma from array

David Carr

Laravel Framework Tutorials PHP & MySQL

Using the $loop variable ( The new $loop variable in Laravel 5.3 ) inside of a loop it’s easy to check when you're at the start or the end of a loop. In this case show a comma for all items except the last one we can use:

! $loop->last 

Which says then the loop is not the last item, at which points add the comma and close it with an endif.

@foreach($items as $item)
    
    {{ $item }}@if (!$loop->last),@endif

@endforeach

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.