Laravel collection implode method like php implode()

What is use of laravel collection implode() function. php array impolde like function in laravel collection. how to change a collection values to comma seperate string.


Laravel collection implode function

laravel collection implode() function work same as php implode function it join collection items by given string and change collection values to string. see below example fruits collection change in comma seperate string.

$collection = collect(['apple', 'mango', 'banana']);
 
$fruits = $collection->implode(', ');
dd($fruits);

// apple, mango, banana

Laravel implode() function on array of collection

when our collection have multilevel arrays then pass key name which you want to join

$collection = collect([
	['id' => 1, 'product' => 'phone'],
	['id' => 2, 'product' => 'laptop'],
]);
 
$products = $collection->implode('product', ', ');
dd($products);

// phone, laptop
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links