Home  >  Article  >  Backend Development  >  How to add new value to collection in Laravel?

How to add new value to collection in Laravel?

PHPz
PHPzforward
2023-09-11 11:53:02867browse

How to add new value to collection in Laravel?

Collection in Laravel is an API wrapper that helps you handle different operations performed on arrays. It uses the Illuminate\Support\Collection class to handle arrays in Laravel.

To create a collection from a given array, you need to use the collect() helper method, which returns a collection instance. You can then sort the collection using a series of methods on the collection instance, such as convert to lowercase.

The Chinese translation of

Example 1

is:

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 a browser you will get the following output −

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

To add a new value, you can use the push() or put() method on the collection.

Example 2

Use 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

Use put() method

When you have a collection with key:value pairs, use the put() method

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

Let us use the put() method to add a key-value pair 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] =>
)
The Chinese translation of

Example 4

is:

Example 4

Push using a collection with array values.

<?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] =>
)

The above is the detailed content of How to add new value to collection in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete