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:
$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.";
}
$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.