How to check if all elements in a collection satisfy a condition in illuminate/collections?

To check if all elements in a collection satisfy a condition using the Illuminate/Collections library in Laravel, you can use the every() method.

Here is an example of how to use the every() method:

use Illuminate\Support\Collection; $collection = collect([1, 2, 3, 4, 5]); $allGreaterThanZero = $collection->every(function ($item, $key) { return $item > 0; }); var_dump($allGreaterThanZero); // Output: true

In this example, the every() method is used to check if all elements in the collection are greater than zero. The callback function passed to every() receives the individual values and keys of the collection, and it should return a boolean value indicating whether the element satisfies the condition.

If all elements in the collection satisfy the condition, every() will return true. Otherwise, it will return false.