You could, But that would be more of a hack (I.e by making some variable global) than an actually good solution. Just use the foreach constraint of php instead.
The each()-method is just a simple wrapper over a foreach, to add some "syntactic sugar" if you need to apply something to all elements in a collection.
You might want map
use Illuminate\Support\Collection;
$collection = new Collection([1,2,3]);
$doubledValues = $collection->map(function($value) {
return $value * 2;
});
//$doubledValues->toArray() is now [2,4,6]
//$collection->toArray() is still [1,2,3]
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community