How to Merge Eloquents in Laravel 8 Collection

Reading Time: 4 minutes
2,295 Views

Inside this article we will see the use of merge() and all() methods in laravel 8 collections. Article contains a very classified information about the basic concept of How to merge Eloquents in laravel 8 collection.

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.

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]);

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

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.

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

<?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;
});

Output


Method #2 Merge Eloquent Collection

In this example we will see how to merge laravel eloquents.

<?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 How to Merge Eloquents in Laravel 8 Collection Tutorial 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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more