Using implode and toArray functions in laravel

Posted 4 months ago by Ryan Dhungel

Category: Laravel

Viewed 290 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>

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()) }}