In this tutorial, we will see the Laravel collection push and put with an example. The Laravel collection push method appends an item to the end of the collection. The put method sets the given key and value in the collection. Let’s do it with an example.
Example 1: Laravel Collection Push Example
In this example, we will add screen
item in the below collection using push()
method.
public function index()
{
$collection = collect(['keyboard', 'mouse', 'pendrive']);
$collection->push('screen');
$collection->all();
dd($collection);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => keyboard
[1] => mouse
[2] => pendrive
[3] => screen
)
)
You can see a new element screen
has been added at the end of the collection
Example 2: Laravel Collection Push With Array Example
We will use the same example as above but this time we will use the Collection array with multiple dimensional.
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'keyboard'],
['id' => 2, 'name' => 'mouse'],
['id' => 3, 'name' => 'pendrive'],
]);
$collection->push(['id' => 4, 'name' => 'screen']);
$collection->all();
dd($collection);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[id] => 1
[name] => keyboard
)
[1] => Array
(
[id] => 2
[name] => mouse
)
[2] => Array
(
[id] => 3
[name] => pendrive
)
[3] => Array
(
[id] => 4
[name] => screen
)
)
)
Example 3: Laravel Collection Add Item With Key Value Pair
In this example, we will see adding key-value pair using put()
method.
public function index()
{
$collection = collect(['product_id' => 1, 'name' => 'Desk']);
$collection->put('price', 100);
$collection->all();
dd($collection);
}
Output:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[product_id] => 1
[name] => Desk
[price] => 100
)
)
You can see a new price
with value 100
has been added.
Additionally, read our guide:
- Laravel: Blade Switch Case Statement Example
- Laravel: Switch Case Statement In Controller Example
- Laravel: Change Column Type In Migration
- Laravel: Change Column Name In Migration
- How To Use Where Date Between In Laravel
- How To Add Laravel Next Prev Pagination
- Laravel Remove Column From Table In Migration
- Laravel: Get Month Name From Date
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Update Pivot Table In Laravel
- How To Install Vue In Laravel 8 Step By Step
- How To Handle Failed Jobs In Laravel
- Best Ways To Define Global Variable In Laravel
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
- Laravel Run Specific Migration
- Redirect By JavaScript With Example
- How To Schedule Tasks In Laravel With Example
- Warning: SPDX license identifier not provided in source file
That’s it from our end. We hope this article helped you to learn the Laravel collection push and put method with examples.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!