Laravel contains() & containsStrict(): Check if Laravel Collection contains Value

Laravel contains() & containsStrict(): Check if Laravel Collection contains Value

Today, we will review Laravel contains() method and Laravel containsStrict() method which is used to determine whether the collection contains a given value or not. This method applies to both Laravel Collection as well as Eloquent Collection object.

Today, we will review Laravel contains() method and Laravel containsStrict() method which is used to determine whether the collection contains a given value or not. This method applies to both Laravel Collection as well as Eloquent Collection object.

Laravel contains() Method

contains() method checks whether Laravel Collections contains certain given value or not. If you pass one value to the contains() method, it will check whether collection has any value matching to the parameter.

Another option is - You can also pass key / value parameter ~ first parameter is key and second parameter is value and it will match for the pair exclusively.

Check if Laravel Collection contains value

Let's review an example with one parameter (checking only value):

$users = collect(['name' => 'John Doe', 'email' => '[email protected]']);

$users->contains('John Doe');

// true

$users->contains('John Admin');

// false

Check if Laravel Collection contains key/value pair

Let's review an example with two parameters (checking whether collection contains key / value pair):

$collection = collect([
            ['id' => '200', 'amount' => '500'],
            ['name' => '201', 'country' => '200'],
        ]);

$collection->contains('amount', '500');
//true

$collection->contains('id','500');
//false

Also Read: Laravel Pluck() Method with Example

Check if Laravel Collection contains value using callback function

Lastly, you can also pass callback function with your own matching expression to check whether laravel collection or eloquent collection contains given value or not.

laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

How to Hire Laravel Developers and How Much Does It Cost.

Want to make the most out of the latest Laravel technology? Here is your guide on how to hire the best Laravel developer, and at the right price. 

How to perform migration rollback in laravel

Sometimes we need to rollback one specific migration. Here, in this article we will discuss about the migration rollback in laravel.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→