Published on November 30, 2016
pull($key, $default = null)
pull method will remove an item from the collection while returning its value. If the item is not in the collection, the optional
$default value will be returned. The
pull method will modify the original collection.
1<?php23use Illuminate\Support\Collection;45// Create a new collection instance.6$collection = new Collection([7 'first' => 'I am first',8 'second' => 'I am second',9 'third' => 'I am third'10]);1112// Pull the third value from the collection.13$value = $collection->pull('third');1415// null16$doesNotExist = $collection->pull('non-existent');1718// default-value19$alsoDoesNotExist = $collection->pull('non-existent', 'default-value');
After the above code has executed, the
$value variable would contain the value
I am third and the
$collection variable would contain a value similar to the following output:
1object(Illuminate\Support\Collection)2 protected 'items' =>3 array (size=2)4 'first' => string 'I am first' (length=10)5 'second' => string 'I am second' (length=11)
Learning text parsing, manipulation, encoding and more through the Laravel string helpers.
Published on November 20, 2016
Published on December 6, 2017
Published on October 25, 2014