Laravel 5 Collections: Filtering Collections With (Without Losing Filtered Rejections) partition

3 min read

The partition method is similar to the filter method, in that it allows you to get a subset of a collection based on some truth condition. However, unlike the filter method, the partition method does not discard the elements that failed the truth condition. Instead, the partition method returns an array of two collections, where the left-hand collection contains the elements that passed the truth condition, and the right-hand side contains the elements that failed the truth condition.

The partition method is useful when used together with PHP's list function.


1public function partition(
2 $callback

Example Use

The following is a trivial example of the partition where we create two partitions, one containing the vowels of a sentence and the other containing the consonants of the sentence. We will then take the results of the partition method and pass them into PHP's list function to create two new local variables: $vowels and $consonants.

1$string = 'The quick brown fox jumped over the lazy dog.';
3list($vowels, $consonants) = collect(str_split($string))->
4 partition(function ($char) {
5 $vowels = 'aeiou';
7 return Str::contains(
8 $vowels,
9 mb_strtolower($char)
10 );
11 });

After the above code has executed, there would be two new local variables containing either the vowels or consonants in the order they appeared in the sentence. However, because of the way the partition method works, and our conditional, anything that is not a vowel will be contained in the $consonants collection (including whitespace and other special characters).

Sentence Vowels:

Index Value
2 e
5 u
6 i
12 o
17 o
21 u
24 e
27 o
29 e
34 e
37 a
42 o

Sentence Consonants:

Index Value
0 T
1 h
4 q
7 c
8 k
10 b
11 r
13 w
14 n
16 f
18 x
20 j
22 m
23 p
25 d
28 v
30 r
32 t
33 h
36 l
38 z
39 y
41 d
43 g
44 .

Using partition With Higher Order Messages

The partition method can also be used with higher order messaging, which allows us to invoke collection methods and access object instance properties using PHP's property accessors syntax. In the reject section, we created a Product class that was used to reject all products that were not on sale.

We will do a similar thing in the following example, except we will create two local variables $onSale and $notOnSale using the partition method and the list function:

1list($onSale, $notOnSale) = collect([
2 [
3 'name' => 'Office Chair',
4 'price' => 399.99,
5 'onSale' => false
6 ],
7 [
8 'name' => 'Desk',
9 'price' => 199.34,
10 'onSale' => true
11 ]
12])->transform(function ($item) {
13 $product = new Product;
14 $product->name = $item['name'];
15 $product->price = $item['price'];
16 $product->onSale = $item['onSale'];
18 return $product;

Thanks for taking the time to read this post! If you found this article useful and want to help support more work like this, please consider sponsoring my work on GitHub, or by checking out some merch.

 Sponsor on GitHub  Shop Merch