Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Basic Collection Example
Here's how to create a basic collection ?
<?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);
}
}
?>
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.
Using push() Method
The push() method adds a new value to the end of the collection ?
<?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);
}
}
?>
Illuminate\Support\Collection Object(
[items:protected] => Array(
[0] => Andria
[1] => Josh
[2] => James
[3] => Miya
[4] => Henry
[5] => Heena
)
[escapeWhenCastingToString:protected] =>
)
Using put() Method
The put() method is used when you have a collection with a key: value pair ?
<?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);
}
}
?>
Illuminate\Support\Collection Object(
[items:protected] => Array(
[firstname] => Siya
[lastname] => Khan
[address] => xyz
[age] => 30
)
[escapeWhenCastingToString:protected] =>
)
Adding Array to Collection
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);
}
}
?>
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] =>
)
Conclusion
Laravel collections provide flexible ways to add new values using push() for appending items and put() for key-value pairs. Both methods modify the collection in place and return the collection instance for method chaining.
