Skip to content

Commit 8b46889

Browse files
committed
document reduce many
1 parent ebe035c commit 8b46889

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

collections.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ For the majority of the remaining collection documentation, we'll discuss each m
164164
[put](#method-put)
165165
[random](#method-random)
166166
[reduce](#method-reduce)
167+
[reduceMany](#method-reduce-many)
167168
[reject](#method-reject)
168169
[replace](#method-replace)
169170
[replaceRecursive](#method-replacerecursive)
@@ -1725,6 +1726,25 @@ The `reduce` method also passes array keys in associative collections to the giv
17251726

17261727
// 4264
17271728

1729+
<a name="method-reduce-many"></a>
1730+
#### `reduceMany()` {#collection-method}
1731+
1732+
The `reduceMany` method reduces the collection to an array of values, passing the results of each iteration into the subsequent iteration. This method is similar to the `reduce` method; however, it can accept multiple initial values:
1733+
1734+
```php
1735+
[$creditsRemaining, $batch] = Image::where('status', 'unprocessed')
1736+
->get()
1737+
->reduceMany(function ($creditsRemaining, $batch, $image) {
1738+
if ($creditsRemaining >= $image->creditsRequired()) {
1739+
$batch->push($image);
1740+
1741+
$creditsRemaining -= $image->creditsRequired();
1742+
}
1743+
1744+
return [$creditsRemaining, $batch];
1745+
}, $creditsAvailable, collect());
1746+
```
1747+
17281748
<a name="method-reject"></a>
17291749
#### `reject()` {#collection-method}
17301750

0 commit comments

Comments
 (0)