How to check if any element in a collection satisfies a condition in illuminate/collections?

In Illuminate/Collections, you can use the contains method with a closure or a callback function to check if any element in a collection satisfies a condition.

Here's an example:

  1. Using a Closure:
$collection = collect([1, 2, 3, 4, 5]); $contains = $collection->contains(function ($value, $key) { return $value > 3; }); if ($contains) { echo "At least one element satisfies the condition."; } else { echo "No element satisfies the condition."; }
  1. Using a Callback Function:
$collection = collect([1, 2, 3, 4, 5]); $contains = $collection->contains(fn ($value) => $value > 3); if ($contains) { echo "At least one element satisfies the condition."; } else { echo "No element satisfies the condition."; }

Both examples will output "At least one element satisfies the condition" since the collection contains elements greater than 3.