How to add a new value to a collection in Laravel?


Collection in Laravel is an API wrapper that helps you deal with different operations to be performed on arrays. It makes use of the class Illuminate\Support\Collection to deal with arrays in Laravel.

To create a collection from a given array you need to make use of the collect() helper method that returns a collection instance. Later you can use a chain of methods like converting to lowercase, sorting on the collection instance.

Example 1

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller{ public function index() { $mynames = collect(['Andria', 'Josh', 'James', 'Miya', 'Henry']); print_r($mynames); } }

Output

When you test the same in the browser you get following output −

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [0] => Andria
      [1] => Josh
      [2] => James
      [3] => Miya
      [4] => Henry
   )
   [escapeWhenCastingToString:protected] =>
)

To add new value you can make use of push() or put() methods on a collection.

Example 2

Using the push() method.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller{ public function index() { $mynames = collect(['Andria', 'Josh', 'James', 'Miya', 'Henry']); $mynames->push('Heena'); print_r($mynames); } }

Output

The output of the above code is −

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [0] => Andria
      [1] => Josh
      [2] => James
      [3] => Miya
      [4] => Henry
      [5] => Heena
   )
   [escapeWhenCastingToString:protected] =>
)

Example 3

Using put() method

The put() method is used when you have a collection with a key: value pair

['firstname' => 'Siya', 'lastname' => 'Khan', 'address'=>'xyz']

Let us make use of the put() method to add one more key: value to the above collection.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller{ public function index() { $stdDetails = collect(['firstname' => 'Siya', 'lastname' => 'Khan', 'address'=>'xyz']); $stdDetails->put('age','30'); print_r($stdDetails); } }

Output

The output of the above code is −

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [firstname] => Siya
      [lastname] => Khan
      [address] => xyz
      [age] => 30
   )
   [escapeWhenCastingToString:protected] =>
)

Example 4

Using push on collection with an array value.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller{ public function index() { $myNames = collect([ ['userid'=>1, 'name'=>'Andria'], ['userid'=>2, 'name'=>'Josh'], ['userid'=>3, 'name'=>'James'] ]); $myNames->push(['userid'=>4, 'name'=>'Miya']); print_r($myNames); } }

Output

The output of the above code is −

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [0] => Array(
         [userid] => 1
         [name] => Andria
      )

      [1] => Array(
         [userid] => 2
         [name] => Josh
      )

      [2] => Array(
         [userid] => 3
         [name] => James
      )

      [3] => Array(
         [userid] => 4
         [name] => Miya
      )
   )
   [escapeWhenCastingToString:protected] =>
)

Updated on: 30-Aug-2022

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements