Laravel Collection Public API: pop

November 30, 2016 —John Koster

pop

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 following sample demonstrates the use of the pop method:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 'first', 'second', 'third'
8]);
9 
10// third
11$third = $collection->pop();

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

1object(Illuminate\Support\Collection)[133]
2 protected 'items' =>
3 array (size=2)
4 0 => string 'first' (length=5)
5 1 => string 'second' (length=6)

Some absolutely amazing
people

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.