Laravel 10 Collection push() and put() Methods Tutorial

Reading Time: 5 minutes
145 Views

The Collection class in Laravel 10 provides a comprehensive set of methods for easily manipulating and transforming data. Push() and put() are two basic Collection class methods. These methods allow you to add elements to a collection, allowing for dynamic data management and application flexibility.

We are going to look at the push() and put() methods in Laravel 10 Collections in this article. We will walk you through the step-by-step process of using these methods to efficiently add pieces to your collections.

Read More: Methods To Create Database in CodeIgniter 4 Tutorial

These methods provide a seamless approach to edit and extend your collections, whether you need to append a single item or combine a full array of data.

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

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.

Collection push() Method

We will see how to use push() into collection.

Read More: CodeIgniter 4 How to Print or Get Last Executed Query

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

Code Example #1

Laravel Collection push() with single dimensional array.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $collection = collect([
            "India",
            "Australia",
            "South Africa",
            "Nepal"
        ]);

        $collection->push("New Zealand");

        $collection->all();

        dd($collection);
    }
}

Concept

$collection->push("New Zealand");

Output

Code Example #2

Laravel Collection push() with multi dimensional array.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $collection = collect([
            ["id" => 1, "name" => "Sanjay", "email" => "sanjay@gmail.com"],
            ["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com"],
            ["id" => 3, "name" => "Ashish", "email" => "ashish@gmail.com"]
        ]);

        $collection->push(['id' => 4, 'name' => 'Dhananjay Negi', 'email' => 'dj@gmail.com']);

        $collection->all();

        dd($collection);
    }
}

Concept

$collection->push(['id' => 4, 'name' => 'Dhananjay Negi', 'email' => 'dj@gmail.com']);

Read More: Convert Image To The Base64 String Using Javascript

Output

Collection put() Method

We will see how to use put() into collection. It used to add a new key value pair to collection data.

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()
    {
        $collection = collect([
            "id" => 1,
            "name" => "Sanjay",
            "email" => "sanjay@gmail.com"
        ]);

        $collection->put('designation', 'Web Developer');

        $collection->all();

        dd($collection);
    }
}

Concept

$collection->put('designation', 'Web Developer');

Output

We hope this article helped you to Laravel 10 Collection push() and put() Methods Tutorial in a very detailed way.

Read More: How To Get Multiple Checkbox Value In jQuery Tutorial

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.