Using implode and toArray functions in laravel
Posted 5 years ago by Ryan Dhungel
Category: Laravel
Viewed 5427 times
Estimated read time: 1 minute
How to use implode and toArray functions to replace foreach loop in laravel?
Lets say in you view instead of using foreach to loop through each lesson tags using the typical method like this:
@foreach ($lesson->tags as $tag)
<p>{{ $tag->name }}</p>
@endforeach
You can use implode function and pluck out all the tags to array and display seperating then with comma:
{{ implode(', ', $lesson->tags->pluck('name')->toArray()) }}