By John Koster
The eachSpread
method is used to execute the provided $callback
function on each element in the collection. The element's values will become arguments to the callback function.
#Signature
1public function eachSpread(
2 callable $callback
3);
#Example Use
The following example demonstrates the use of the eachSpread
method:
1$products = collect([
2 [
3 'Office Chair',
4 399.99
5 ],
6 [
7 'Desk',
8 199.34
9 ]
10]);
11
12$products->eachSpread(function ($name, $price) {
13 // We have access to the name and price
14 // of each product within the callback.
15});
If, at some point, you would like to stop iterating the collection, simply return false
from the callback function:
1$products->eachSpread(function ($name, $price) {
2 if ($price > 1000) {
3 // Returning false stops iteration.
4 return false;
5 }
6});
∎