Using implode and toArray functions in laravel


Posted 8 months ago by Ryan Dhungel

Category: Laravel

Viewed 458 times

Estimated read time: 1 minute

Course of the day!

Learn Laravel Building Multi User Blogging Platform with SEO

Learn Laravel Building Multi User Blogging Platform with SEO

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