Home » Laravel Collection Push() And Put() With Example

Laravel Collection Push() And Put() With Example

Last updated on May 18, 2022 by

In this tutorial, we will see the Laravel collection push and put with an example. The Laravel collection push method appends an item to the end of the collection. The put method sets the given key and value in the collection. Let’s do it with an example.

Example 1: Laravel Collection Push Example

In this example, we will add screen item in the below collection using push() method.

public function index()
{
    $collection = collect(['keyboard', 'mouse', 'pendrive']);

    $collection->push('screen');

    $collection->all();

    dd($collection);
}

Output:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => keyboard
            [1] => mouse
            [2] => pendrive
            [3] => screen
        )
)

You can see a new element screen has been added at the end of the collection

Example 2: Laravel Collection Push With Array Example

We will use the same example as above but this time we will use the Collection array with multiple dimensional.

public function index()
{
    $collection = collect([
            ['id' => 1, 'name' => 'keyboard'],
            ['id' => 2, 'name' => 'mouse'],
            ['id' => 3, 'name' => 'pendrive'],
        ]);

    $collection->push(['id' => 4, 'name' => 'screen']);

    $collection->all();

    dd($collection);
}

Output:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => keyboard
                )
            [1] => Array
                (
                    [id] => 2
                    [name] => mouse
                )
            [2] => Array
                (
                    [id] => 3
                    [name] => pendrive
                )
            [3] => Array
                (
                    [id] => 4
                    [name] => screen
                )
        )
)

Example 3: Laravel Collection Add Item With Key Value Pair

In this example, we will see adding key-value pair using put() method.

public function index()
{
    $collection = collect(['product_id' => 1, 'name' => 'Desk']);
 
    $collection->put('price', 100);
     
    $collection->all();

    dd($collection);
}

Output:

Illuminate\Support\Collection Object

(
    [items:protected] => Array
        (
            [product_id] => 1
            [name] => Desk
            [price] => 100
        )
)

You can see a new price with value 100 has been added.

Additionally, read our guide:

  1. Laravel: Blade Switch Case Statement Example
  2. Laravel: Switch Case Statement In Controller Example
  3. Laravel: Change Column Type In Migration
  4. Laravel: Change Column Name In Migration
  5. How To Use Where Date Between In Laravel
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Laravel: Get Month Name From Date
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Update Pivot Table In Laravel
  11. How To Install Vue In Laravel 8 Step By Step
  12. How To Handle Failed Jobs In Laravel
  13. Best Ways To Define Global Variable In Laravel
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel
  16. How To Pass Laravel URL Parameter
  17. Laravel Run Specific Migration
  18. Redirect By JavaScript With Example
  19. How To Schedule Tasks In Laravel With Example
  20. Warning: SPDX license identifier not provided in source file

That’s it from our end. We hope this article helped you to learn the Laravel collection push and put method with examples.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment