Laravel 5 Collections: Determining If a Collection Has An Element With has

April 22, 2018 —John Koster

The has method is used to determine if a item exists in the collection based on a given $key. The has method returns true if the item exists and false if it does not.

#Signature

1public function has(
2 $key
3);

#Example Use

The following example demonstrates the usage of the has method:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 'first' => 'I am first',
6 'second' => 'I am second'
7]);
8 
9// true
10$collection->has('first');
11 
12// true
13$collection->has('second');
14 
15// false
16$collection->has('third');

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.