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<?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); } }
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.
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); } }
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] => )
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); } }
The output of the above code is -
Illuminate\Support\Collection Object( [items:protected] => Array( [firstname] => Siya [lastname] => Khan [address] => xyz [age] => 30 ) [escapeWhenCastingToString:protected] => )
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); } }
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!