Published on November 30, 2016
push method is the logical opposite of the
prepend method and will push an item onto the end of the collection. The
push method returns a reference to the original collection instance.
The following code example shows how to use the
push method to put an item on the end of the collection:
1<?php23use Illuminate\Support\Collection;45// Create a new collection instance.6$collection = new Collection([7 'I am first',8 'I am second'9]);1011// Push an item onto the collection.12$collection->push('I am third');
After the above code has executed, the
$collection variable would contain a value similar to the following output:
1object(Illuminate\Support\Collection)2 protected 'items' =>3 array (size=3)4 0 => string 'I am first' (length=10)5 1 => string 'I am second' (length=11)6 2 => string 'I am third' (length=10)
It should be noted that the
push method cannot be used to set the key of the item that is being added to the collection.
Learning text parsing, manipulation, encoding and more through the Laravel string helpers.
Published on April 22, 2018
Published on September 4, 2014
Published on April 21, 2018