Laravel Collections – contains() & containsStrict() Methods
Laravel Collections ( 11 Lessons )
Laravel Collections is one of the most powerful features of Laravel. Basically, collections are PHP arrays but it’s an Object Oriented approach to deal with PHP arrays.
see full series- Laravel Collections – Introduction to Collections
- Laravel Collections – make Method
- Laravel Collections – wrap() and unwrap() Methods
- Laravel Collections – map() Method
- Laravel Collections – times() Method
- Laravel Collections – collapse() Method
- Laravel Collections – contains() & containsStrict() Methods
- Laravel Collections – each() Method
- Laravel Collection – Using toJson() Method
- Laravel Collection – Filtering Collection Items using where()
- Laravel Collection – Using splice() Method to Remove Items
In this post, we will learn about how to use Laravel Collections contains()
and containsStrict()
method.
contains() Method
The contains()
method determines if a given key exist in the collection. You can also provide the value to check against the key as a second argument, which will check if the given key/value pair exists in the collection.
You can use the contains()
method on collections which contains arrays and objects as well. The contains()
method will return a boolean value.
Signature
Here is the signature of this method in Laravel Collection class.
** * Determine if an item exists in the collection. * * @param mixed $key * @param mixed $operator * @param mixed $value * @return bool */ public function contains($key, $operator = null, $value = null);
Example
Below example shows the most common usage of the contains()
method.
use Illuminate\Support\Collection; // Create a new collection with given values $collection = new Collection([ 'London', 'Paris', 'Tokyo', 'New York', 'Dubai' ]); $collection->contains('Paris'); // true $collection->contains('Manchester'); // false
The following example will demonstrate how to use the contains()
method on a key/value pairs collection which has array items. You can supply the value in the second argument as described in the signature above.
use Illuminate\Support\Collection; // Create a new collection with given values $collection = new Collection([ ['London' => 'United Kingdom'], ['Paris' => 'France'], ['Tokyo' => 'Japan'], ['New York' => 'United Satates'], ['Dubai' => 'United Arab Emirates'], ]); $collection->contains('Tokyo', 'Japan'); // true $collection->contains('London', 'Germany'); // false
Now, we will look at how we can use the conains()
method on a collection which has objects as it’s items.
use Illuminate\Support\Collection; class Product { /** * @var string */ public $name = ''; /** * @var float */ public $price = 0; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } } // Create a new collection instance. $collection = new Collection([ new Product('MacBook Air', 1499), new Product('Lenovo ThinkPad', 799), new Product('Dell Latitude', 599) ]); $collection->contains('name', 'Lenovo ThinkPad'); // true $collection->contains('name', 'MacBook Pro'); // false $collection->contains('price', 599); // true
You can also provide a function as it’s only argument to the contains()
method. The following example will demonstrate how to check if there is any product in the collection above with price more than 1000.
$collection->contains(function($key, $value) { return $value->price > 1000; }); // true
Note that the $value
will be an instance of the Product
class, so you can run the test on it’s properties.
containsStrict() Method
The containsStrict()
method is similar to the contains()
method, but it test the value based on a stric comparison operator. For example, if we have the integer values in our collection and then we provide the string values to containsStrict()
method, it will return false
.
Signature
Here is the signature of this method in Laravel Collection class.
/** * Determine if an item exists in the collection using strict comparison. * * @param mixed $key * @param mixed $value * @return bool */ public function containsStrict($key, $value = null);
Example
We will use the containsStrict()
method on a new collection like below.
use Illuminate\Support\Collection; $collection = new Collection(['99.99', '149.99', '199.99']); $collection->containsStrict(149.99); // false $collection->containsStrict('99.99'); // true
As you can see if we provide the price to check it will return false as we supplied the integer value, not a string. In second test, it returns true
because we supplied the string value.
You can use the containsStrict()
method on values, key/value pairs and objects as we did in the contains()
example.
That’s all for now in Laravel Collections series, if you have any question please leave it in the comment box below.