Laravel Collections

Verify Entire Datasets with Collection::every()

Punyapal Shah 1 min read
edit this tip
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 true if all items pass the condition; returns false immediately on the first failed item (short-circuiting).
  • Returns true on empty collections.
  • Replaces manual foreach loops and state tracking flags.
Tags: Laravel Collections Validation Clean Code
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips