Search

Laravel 5 Collections: Retrieving a Portion of a Collection With slice

April 22, 2018 —John Koster

The slice method is used to return a slice, or portion of the collection starting at the given $offset. The $offset tells the slice method where to begin when creating the new collection. For example, if the $offset is 3, the new collection will begin with the third item in the original collection (the collection the slice method was invoked on). If the $offset is negative, the new collection will start that distance from the end of the original collection. The slice method returns a new Collection instance and will not modify the original Collection instance.

The slice method is similar in behavior to PHP's array_slice function.

#Signature

1public function slice(
2 $offset,
3 $length = null
4);

#Example Use

The following code example demonstrates the usage of the slice method using a simple collection:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([1, 2, 3, 4, 5 , 6, 7, 8]);
5 
6// Slice the collection starting at the third item.
7// The returned collection will contain items
8// 4, 5, ,7 and 8.
9$sliced = $collection->slice(3);

After the above code has executed the $sliced variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 4
5 1 => int 5
6 2 => int 6
7 3 => int 7
8 4 => int 8

The same result can be accomplished using a negative $offset like so:

1// Slice the collection starting starting at the
2// fifth item from the end of the collection. The
3// returned collection will contain the items
4// 4, 5, 6, 7 and 8.
5$sliced = $collection->slice(-5);

After the above code has executed, like before, the $sliced variable would be an instance of Collection and would contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 4
5 1 => int 5
6 2 => int 6
7 3 => int 7
8 4 => int 8

The $length parameter allows developers to control the size of the returned Collection. If the provided $length is positive, the returned collection will have up that many items. The total number of items can be less than the supplied $length if the original collection does not have enough items to satisfy the $length. If the $length is negative, the returned collection will stop that many items from the end of the original collection. The following example shows the various uses of the $length parameter using the $collection instance created earlier:

1// The returned collection will contain the items
2// 2, 3 and 4.
3$sliced = $collection->slice(1, 3);
4 
5// The returned collection will contain the items
6// 1, 2 and 3.
7$sliced = $collection->slice(0, 3);
8 
9// The returned collection will contain the items
10// 1, 2, 3, 4 and 5.
11$sliced = $collection->slice(0, -3);

By default, the slice method will reorder and reset the keys of the returned collection's items. This can be changed by passing true as the argument for the $preserveKeys parameter. The following code samples show the affect of the $preserveKeys parameter:

1// Slice the collection without preserving keys.
2$sliced = $collection->slice(2, 3);
3 
4// Slice the collection while preserving keys.
5$preservedKeySlice = $collection->slice(2, 3, true);

After the above code has executed, the $sliced variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 3
5 1 => int 4
6 2 => int 5

Notice that the keys of the collection are 0, 1 and 2. The following output is for the $preservedKeySlice variable (which is also an instance of Collection). Notice how the keys are 2, 3 and 4, because they were preserved:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 2 => int 3
5 3 => int 4
6 4 => int 5

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.