How to use contains on collection in laravel?

Member

by gilbert , in category: Third Party Scripts , 10 days ago

How to use contains on collection in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 18 hours ago

@gilbert 

In Laravel, you can use the contains method to determine if a collection contains a specific item.


Here is an example of how to use contains on a collection in Laravel:

1
2
3
4
5
6
7
$collection = collect([1, 2, 3, 4, 5]);

if ($collection->contains(3)) {
    echo 'Collection contains the value 3';
} else {
    echo 'Collection does not contain the value 3';
}


In this example, we have a collection of numbers and we are checking if the collection contains the value 3. If the value 3 is found in the collection, it will output "Collection contains the value 3", otherwise it will output "Collection does not contain the value 3".