Laravel 5 Collections: Retrieving and Removing the Last Collection Element With pop

April 22, 2018 —John Koster

The pop method is used to retrieve the last item from the collection while also removing it from the collection. If there are no items in the collection, the pop method will return null. The pop method modifies the collection instance it is invoked on.

The pop exhibits similar behavior to PHP's array_pop function.


1public function pop();

#Example Use

The following sample demonstrates the use of the pop method:

1use Illuminate\Support\Collection;
3// Create a new collection instance.
4$collection = new Collection([
5 'first', 'second', 'third'
8// third
9$third = $collection->pop();

The pop method alters the original collection, so the $collection variable would now contain a value similar to the following output:

2 protected 'items' =>
3 array
4 0 => string 'first'
5 1 => string 'second'

Some absolutely amazing

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.