Laravel 9 How to Concat Multiple Collections Tutorial

Reading Time: 3 minutes
1,000 Views

Inside this article we will see the use of concat() method in laravel 9 collections. Article contains a very classified information about the basic concept of Laravel 9 How to Concat Multiple Collections Tutorial.

We will concat two or more collections in laravel.

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: jQuery Remove All Numbers From String Example Tutorial

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.

Use of Collection concat() Method

We will see how to use concat() method of collection.

Read More: Laravel 9 Time Format into AM PM Using Carbon Tutorial

Suppose we have SiteController.php a controller file inside /app/Http/Controllers folder.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $collectionA = collect(["One", "Two"]);
        
        $collectionB = collect(["Three", "Four"]);
        
        $collectionC = collect(["Five", "Six"]);
      
        $collection = $collectionA->concat($collectionB)->concat($collectionC);
            
        dd($collection);
    }
}

Concept

$collection = $collectionA->concat($collectionB)->concat($collectionC);

Output

We hope this article helped you to learn Laravel 9 How to Concat Multiple Collections Tutorial in a very detailed way.

Read More: How To Download File Using jQuery Example Tutorial

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.