- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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] => )
- Related Articles
- How to add a new column to an existing table of Laravel in a migration?
- How to add a new field to all the documents in a MongoDB collection
- Add new field to every document in a MongoDB collection?
- How to check if a Laravel collection is empty?
- Script to add a single value to array in MongoDB collection?
- How to create a new collection in MongoDB?
- How to add a new value to each element of list in R?
- How to Insert a value to a hidden input in Laravel Blade?
- How to add a column in MongoDB collection?
- How to add new value to an existing array in JavaScript?
- Add MD5 hash value to MongoDB collection?
- Add a new value to a column of data type enum in MySQL?
- How to add a new column to a matrix in R?
- How to add new keys to a dictionary in Python?
- MySQL SELECT to add a new column to a query and give it a value?
