Merging collections in Laravel

In this article we will see how to merge Eloquent collections in Laravel using an example. Our example we will use the Laravel collection merge by value. The merge method will return a new instance of collection without making any changes to the original Collection instance. In our first Collection keys are numeric, second will be added to the end of new collection’s. Merge method accepts only an array or a Collection instance. 

 /**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(){
    $old = collect(['buysycle']);

    $new = collect(['car']);

    $merged = $old->merge($new);

    dd($merged->toarray());
}
The $merged variable will be an instance of Collection and contain a value similar to the below output.
array:2 [?

  0 => "buysycle"

  1 => "car"

]