How to Use Contains on Collection In Laravel?

3 minutes read

In Laravel, you can use the contains method on a collection to check if the collection contains a specific value. This method will return true if the value is found in the collection, and false otherwise.


To use the contains method, simply call it on the collection and pass in the value you want to check for. For example, if you have a collection of numbers $numbers and you want to check if it contains the number 5, you can do so like this:

1
2
3
4
$numbers = collect([1, 2, 3, 4, 5]);
if ($numbers->contains(5)) {
    // Do something if 5 is found in the collection
}


You can also use the contains method with a callback function to perform more advanced checks. For example, if you have a collection of users $users and you want to check if any user has a specific email address, you can do so like this:

1
2
3
4
5
6
$users = User::all();
if ($users->contains(function ($user) {
    return $user->email === 'example@example.com';
})) {
    // Do something if a user with the specific email address is found in the collection
}


Overall, the contains method is a convenient way to check if a collection contains a specific value or meets certain conditions in Laravel.


What is the return value of contains method in Laravel collection?

The contains method in Laravel collection returns a boolean value, either true or false. It indicates whether the collection contains a given key or value.


How to handle case sensitivity when using contains method in Laravel collection?

To handle case sensitivity when using the contains method in Laravel collection, you can utilize the filter method along with a custom closure that performs a case-insensitive comparison. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$collection = collect(['Apple', 'Banana', 'Orange']);

$searchTerm = 'apple';

$filteredCollection = $collection->filter(function ($item) use ($searchTerm) {
    return stripos($item, $searchTerm) !== false;
});

if ($filteredCollection->isNotEmpty()) {
    // Your desired action if the search term is found
} else {
    // Your desired action if the search term is not found
}


In this example, the filter method is used to iterate over each item in the collection and perform a case-insensitive comparison using stripos. If the search term is found in any item, it will be included in the filtered collection. You can then check if the filtered collection is not empty to determine if the search term was found or not.


How to create a custom contains method for Laravel collection?

To create a custom contains method for a Laravel collection, you can extend the Collection class and add your own method. Here's an example of how you can do this:

  1. Create a new class that extends the Illuminate\Support\Collection class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

namespace App\Support;

use Illuminate\Support\Collection;

class CustomCollection extends Collection
{

}


  1. Add a custom contains method to your new class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public function customContains($value)
{
    foreach ($this->items as $item) {
        if ($item === $value) {
            return true;
        }
    }

    return false;
}


  1. Now you can use your custom contains method on any collection instance:
1
2
3
4
5
6
7
$collection = new CustomCollection([1, 2, 3, 4, 5]);

if ($collection->customContains(3)) {
    echo 'Value found in collection';
} else {
    echo 'Value not found in collection';
}


By following these steps, you can create a custom contains method for Laravel collection and use it in your applications as needed.

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

To sort a Laravel collection by distance from a specific location, you can use the sortBy method along with a custom callback function.First, calculate the distance between each item in the collection and the target location using a formula such as the Haversi...
To run Laravel on XAMPP without using Artisan, you will need to manually set up the project in your XAMPP environment. First, make sure that your XAMPP server is running properly. Then, navigate to the htdocs folder in your XAMPP directory and create a new fol...
To send a blade as an attachment in PDF format in Laravel, you can follow these steps:Create a new blade file that you want to send as a PDF attachment.Use the mPDF library or any other PDF generation package to convert the blade file into a PDF format.Save th...
To make an AJAX request in Laravel, you can use Laravel&#39;s built-in CSRF protection and use any JavaScript library like jQuery to send the request. You first need to include the CSRF token in your AJAX request header as Laravel uses this token to verify tha...
To send multiple values in Twilio in Laravel, you can use the Twilio SDK in your Laravel application. First, include the Twilio SDK in your Laravel project using Composer. Next, create a new Twilio client using your Twilio account SID and auth token. Then, use...