The Collection class in Laravel 10 provides a variety of useful methods for interacting with data collections. The Collection class has two crucial methods: contains() and containsStrict(). These methods allow you to check whether a collection includes a certain value, making conditional checks and data validations simple.
We will look at the contains() and containsStrict() methods in Laravel 10 Collections in this article. We will walk you through the process of using these methods to check for the presence of a value in your collections, step by step.
Read More: Laravel 10 Collection push() and put() Methods Tutorial
These methods provide seamless functionality to meet your requirements, whether you need to search for a value based on a specific criteria or do strict type checks.
The Illuminate\Support\Collection
class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We’ll use the collect
helper to create a new collection instance from the array.
As mentioned above, the collect
helper returns a new Illuminate\Support\Collection
instance for the given array.
So, creating a collection is as simple as:
$collection = collect([1, 2, 3]);
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Laravel Collection: Use contains() Method
We will see how to use contains() into collection.
Read More: CodeIgniter 4 How to Print or Get Last Executed Query
Suppose we have SiteController.php a controller file inside /app/Http/Controllers folder.
Code Example #1
Laravel Collection contains() –
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $collection = collect([ "India", "Australia", "South Africa", "Nepal" ]); $collection->contains('Nepal'); /* true */ $collection->contains('China'); /* false */ } }
Concept
$collection->contains('Nepal'); /* true */
Code Example #2
Laravel Collection contains() with Key Value Check
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $collection = collect([ ["id" => 1, "name" => "Sanjay", "email" => "sanjay@gmail.com"], ["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com"], ["id" => 3, "name" => "Ashish", "email" => "ashish@gmail.com"] ]); $collection->contains('name', 'Vijay'); /* true */ $collection->contains('name', 'Dhananjay'); /* false */ } }
Concept
$collection->contains('name', 'Vijay'); /* true */
Read More: Convert Image To The Base64 String Using Javascript
Code Example #3
Laravel Eloquent with Collection contains()
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; class SiteController extends Controller { public function index() { Product::create(['name' => 'Science Books', 'price' => 2200]); Product::create(['name' => 'Tshirts', 'price' => 1250]); Product::create(['name' => 'Jeans', 'price' => 1500]); Product::get()->contains('name', 'Jeans'); /* true */ Product::get()->contains('name', 'Electronic Device'); /* false */ Product::get()->contains('price', 1500); /* true */ } }
Concept
Product::get()->contains('name', 'Jeans'); /* true */
Code Example #4
Laravel Collection contains() with function
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; class SiteController extends Controller { public function index() { Product::create(['name' => 'Science Books', 'price' => 2200]); Product::create(['name' => 'Tshirts', 'price' => 1250]); Product::create(['name' => 'Jeans', 'price' => 1500]); Product::get()->contains(function ($key, $value) { return $value->price > 1000; }); // true } }
Concept
Product::get()->contains(function ($key, $value) {
return $value->price > 1000;
}); // true
Read More: How To Get Multiple Checkbox Value In jQuery Tutorial
Laravel Collection: Use containsStrict() Method
We will see how to use containsStrict() into collection. It checks the value and the type as well.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $collection = collect([100, 150, 200, 250, 300]); $collection->containsStrict('150'); /* false */ $collection->containsStrict(150); /* true */ } }
We hope this article helped you to Laravel 10 Collection contains() and containsStrict() Methods Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.