How to merge two Collections in Laravel?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this example, i will explain with you how to merge two collections in laravel example. Here i will be show you collections in merge method. Now here we have two collections then we will merge both of them by using merge method.

Here we will know about the merge methods. Merges method will give first collection with the second collection in the same collection. We know that the merge method will replace with the first collection in the original of the second collection’s items if a string key also with the same value exists in the supplied $items.

If here the first collection keys are numeric,then the second collection will be added to the end of the new collection’s. So the merge method will be accept either an array or a Collection instance. And also the merge method returns a new instance of Collection and it does not modify the original collection instance.

So here we will see the example of merge method

Example 1

Now here the example of $old collection and $new collection merge

 /**
 * 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());
}
 

So after the above code has executed, the $merged variable will be an instance of Collection and contain a value similar to the following output:

output

 array:2 [?
  0 => "buysycle"
  1 => "car"
]
 
Example 2

So here the example $uses collection and $admins collection merge

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(){
    $users = User::get();
    
    $admins = Admin::get();

    $mergedAdminUser = $admins->merge($users);

    dd($merged->toarray());
}
 

So after the above code has executed, the $mergedAdminUser variable will be an instance of Collection and it contain a value similar to the following output:

output

array:3 [?
  0 => array:6 [?
    "id" => 6
    "name" => "admin"
    "email" => "admin@gmail.com"
    "email_verified_at" => null
    "created_at" => "2020-01-03 10:58:39"
    "updated_at" => "2020-01-03 10:58:39"
  ]
  1 => array:6 [?
    "id" => 7
    "name" => "Akeem Stanton"
    "email" => "kaitlyn88@example.com"
    "email_verified_at" => "2020-01-11 11:31:19"
    "created_at" => "2020-01-11 11:31:19"
    "updated_at" => "2020-01-11 11:31:19"
  ]
  2 => array:6 [?
    "id" => 10
    "name" => "Kayla Sanford"
    "email" => "antonia.gorczany@example.com"
    "email_verified_at" => "2020-01-11 11:31:19"
    "created_at" => "2020-01-11 11:31:19"
    "updated_at" => "2020-01-11 11:31:19"
  ]
]

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →