How To Merge Two Collections In Laravel

$collection = collect(['laravel', 'php']);

$merged = $collection->merge(['jquery', 'javascript']);

$merged->all();
// ['laravel', 'php', 'jquery', 'javascript']
public function index()
{
$collection_1 = collect(['collection 1']);
$collection_2 = collect(['collection 2']); $merge_collection = $collection_1->merge($collection_2); dd($merge_collection->toArray());

// ['collection 1', 'collection 2']
}
$collection = collect(['id' => 1, 'price' => 100]);

$merged = $collection->merge(['price' => 200, 'discount' => 10%]);

$merged->all();

// ['id' => 1, 'price' => 200, 'discount' => 10%]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

96 Followers

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.