Laravel - Collections - Method Takewhile
The takeWhile
method returns items in the collection until the given callback returns false
:
$collection = collect([1, 2, 3, 4]);
$subset = $collection->takeWhile(function ($item) {
return $item < 3;
});
$subset->all();
// [1, 2]
If the callback never returnsfalse
, thetakeWhile
method will return all items in the collection.