In this post, we will see about difference between contains() & containsStrict() Methods
contains() Method #
if we Look contains() method Syntax:
/**
* Determine if an item exists in the enumerable.
*
* @param mixed $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function contains($key, $operator = null, $value = null);
contains() if an element exists in the collection matching a given truth other wise it return false .let see example
$collection = collect([
'apple',
'samsung',
'xiaomi',
'oppo'
]);
$collection->contains('apple'); //true
$collection->contains('htc'); //false
we can see apple is exists then it will return true and htc not exists then it will return false
Problem with contains() method #
Read also #
Laravel php artisan inspire command
Laravel clear cache without using artisan command
Top comments (1)
Great breakdown! The contains() method checks if a value exists in a Laravel collection, making it useful for quick lookups. However, it uses loose comparison, meaning type differences may not matter. For stricter checks, containsStrict() ensures both value and type match. Perfect for avoiding unintended matches!