Search

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

April 22, 2018 —John Koster

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.

#Signature

1public function partition(
2 $callback
3);

#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.';
2 
3list($vowels, $consonants) = collect(str_split($string))->
4 partition(function ($char) {
5 $vowels = 'aeiou';
6 
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
3
4 q
7 c
8 k
9
10 b
11 r
13 w
14 n
15
16 f
18 x
19
20 j
22 m
23 p
25 d
26
28 v
30 r
31
32 t
33 h
35
36 l
38 z
39 y
40
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'];
17 
18 return $product;
19})->partition->isOnSale();

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.