DEV Community

Cover image for Laravel Collection difference between contains() & containsStrict() Methods
saim
saim

Posted on • Originally published at larainfo.com

Laravel Collection difference between contains() & containsStrict() Methods

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);
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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 more...

Read also #

Laravel php artisan inspire command
Laravel clear cache without using artisan command

Top comments (1)

Collapse
 
rimiweb profile image
rimi-web

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!