April 22, 2018 —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.
1public function eachSpread(2 callable $callback3);
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 price14 // 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});
∎
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.