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

How to add new value to collection in Laravel?

PHPz
Release: 2023-09-11 12:02:01
forward
955 people have browsed it

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);
   }
}
Copy after login

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] =>
)
Copy after login

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);
   }
}
Copy after login

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] =>
)
Copy after login

Example 3

Use put() method

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

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

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);
   }
}
Copy after login

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] =>
)
Copy after login
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);
   }
}
Copy after login

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] =>
)
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template