ItSolutionStuff.com

Laravel Collection Check Contains Value Example

By Hardik Savani β€’ April 16, 2024
Laravel

This simple article demonstrates of laravel collection check if key value exists. i explained simply about how to check if key exists in collection laravel. let’s discuss about laravel collection check if value exists. We will use laravel check if collection contains value.

I will give you some examples of how to check value is exists or not in collection in laravel. you can easily add array in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

Let's see example:

Example 1: Laravel Collection Contains Example

public function index()

{

$collection = collect([

'Mumbai',

'New York',

'London',

'Rajkot'

]);

$collection->contains('Rajkot'); /* true */

$collection->contains('Paris'); /* false */

}

Example 2: Laravel Collection Contains with Key Value Check

public function index()

{

$collection = collect([

['id'=>1, 'name'=>'Hardik'],

['id'=>2, 'name'=>'Vimal'],

['id'=>3, 'name'=>'Harshad'],

['id'=>4, 'name'=>'Harsukh'],

]);

$collection->contains('name', 'Harshad'); /* true */

$collection->contains('name', 'Mahesh'); /* false */

}

Example 3: Laravel Eloquent with Collection Contains

public function index()

{

Product::create(['name'=>'Silver', 'price'=>150]);

Product::create(['name'=>'Bronze', 'price'=>250]);

Product::create(['name'=>'Gold', 'price'=>50]);

Product::get()->contains('name', 'Gold'); /* true */

Product::get()->contains('name', 'Red'); /* false */

Product::get()->contains('price', 50); /* true */

}

Example 4: Laravel Collection Contains with function

public function index()

{

Product::create(['name'=>'Silver', 'price'=>150]);

Product::create(['name'=>'Bronze', 'price'=>250]);

Product::create(['name'=>'Gold', 'price'=>50]);

Product::get()->contains(function($key, $value) {

return $value->price > 100;

}); // true

}

Example 5: Laravel Collection Contains with function

public function index()

{

$collection = collect([100, 150, 200, 250, 300]);

$collection->containsStrict('150'); /* false */

$collection->containsStrict(150); /* true */

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel Collection Map Method Example

Read Now β†’
β˜…

Laravel Collection diff(), diffAssoc() and diffKeys() Example

Read Now β†’
β˜…

Laravel Collection Count and CountBy Method Example

Read Now β†’
β˜…

Laravel Collection first() and firstWhere() Methods Example

Read Now β†’
β˜…

Laravel Collection contains() and containsStrict() Methods Example

Read Now β†’
β˜…

Laravel Collection Push() and Put() Example

Read Now β†’
β˜…

Laravel Collection SortByDesc Tutorial with Examples

Read Now β†’
β˜…

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now β†’
β˜…

Laravel Collection Unique | Remove Duplicates from Collection Laravel

Read Now β†’
β˜…

Laravel Collection Search Method Example

Read Now β†’
β˜…

Laravel Collection Filter Method Example

Read Now β†’