Use every() on Laravel Collections to verify whether every single item in a collection satisfies a truth test.
When validating whether all line items in an order are in stock, or confirming all members in a team have completed onboarding, looping and keeping boolean flags is error-prone.
The every() method evaluates a predicate callback across all items.
Basic Usage
$scores = collect([85, 92, 78, 90]);
// Returns true if all scores are >= 70
$allPassed = $scores->every(fn (int $score) => $score >= 70);
Checking Model Collections
$orderItems = $order->items;
// True only if every single item is in stock
$canFulfill = $orderItems->every(fn (OrderItem $item) => $item->quantity <= $item->product->stock);
Truthy Value Checks
If no callback is provided, every() checks if all elements are truthy:
$flags = collect([true, true, true]);
$allTrue = $flags->every(); // true
Summary
- Returns
trueif all items pass the condition; returnsfalseimmediately on the first failed item (short-circuiting). - Returns
trueon empty collections. - Replaces manual
foreachloops and state tracking flags.
Tags:
Laravel Collections Validation Clean Code