This tutorial will show you how to merge collections in Laravel 10. As a Laravel developer, you are aware that dealing with Eloquent models is an essential part of creating powerful and efficient applications. In some cases, you may need to merge data from many Eloquent models into a single cohesive dataset. This is when merging Eloquent models comes in handy.
Article contains quality information about Merge Eloquents in Laravel 10 Collection using Merge Method.
Article contains a very classified information about the basic concept of How to merge Eloquents in laravel. Here, We will merge collections in laravel and also will see how to merge laravel eloquents.
The Illuminate\Support\Collection
class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We’ll use the collect
helper to create a new collection instance from the array.
Read More: How To Get Last Record of Database Table in Laravel 10
As mentioned above, the collect
helper returns a new Illuminate\Support\Collection
instance for the given array.
So, creating a collection is as simple as:
$collection = collect([1, 2, 3]);
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Method #1: Use merge() Method
We will see how to use merge() into collection.
Suppose we have SiteController.php a controller file inside /app/Http/Controllers folder.
Read More: Inertia Js Authentication in Laravel 10 with Jetstream Tutorial
Code Example #1 – Merge Collection with Values
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $collection1 = collect([1, 2, 3]); $collection2 = collect([4, 5]); $mergedCollection = $collection1->merge($collection2); $mergedCollection->all(); dd($mergedCollection); } }
Concept
$mergedCollection = $collection1->merge($collection2);
$mergedCollection->all();
Output
Code Example #2 – Merge Collection with Unique Values
Here,
Suppose we have two collections, but few elements of collections are duplicate. Now, let’s see how to merge unique values only.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $collection1 = collect([1, 2, 3, 4]); $collection2 = collect([4, 5, 5, 6, 7]); $mergedCollection = $collection1->merge($collection2); $mergedCollection = $mergedCollection->unique(function ($item) { return $item; }); $mergedCollection->all(); dd($mergedCollection); } }
Concept
$mergedCollection = $mergedCollection->unique(function ($item) {
return $item;
});
Read More: How To Stop Spam User Registration in WordPress Using Plugin
Output
Method #2: Merge Eloquent Collection
In this example we will see how to merge laravel eloquents.
Let’s say we have two models – Product & Seller
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; use App\Models\Seller; class SiteController extends Controller { public function index() { $collection1 = Product::get(); $collection2 = Seller::get(); $mergedCollection = $collection1->merge($collection2); $mergedCollection->all(); dd($mergedCollection); } }
We hope this article helped you to learn Step-by-Step Guide To Merge Eloquents in Laravel 10 Collection in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.