How to Filter Collection In Laravel?

3 minutes read

To filter a collection in Laravel, you can use the filter() method provided by the Laravel Collection class. This method allows you to iterate over each item in the collection and apply a callback function to determine if the item should be kept in the filtered collection. The callback function should return true to keep the item in the collection and false to remove it. You can also use the where() method if you want to filter the collection based on a specific key-value pair. This method will return a new collection containing only the items that match the specified criteria.


How to filter a Laravel collection by excluding certain items based on a condition?

You can filter a Laravel collection by excluding certain items based on a condition using the reject method. The reject method removes items from the collection that match the given condition.


Here's an example of how you can use the reject method to filter a collection:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$collection = collect([
    ['name' => 'John', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Alice', 'age' => 35],
]);

$filteredCollection = $collection->reject(function ($item) {
    return $item['age'] < 30;
});

$filteredCollection->all();


In this example, we have a collection of arrays where each array represents a person with a name and an age. We use the reject method to filter out people who are younger than 30. The resulting $filteredCollection will only contain people who are 30 years old or older.


You can customize the condition inside the reject method to suit your specific requirements for filtering the collection.


How to filter a Laravel collection based on a specific condition?

To filter a Laravel collection based on a specific condition, you can use the filter() method. Here's an example of how you can filter a collection of users based on a specific condition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$users = User::all(); // Assuming User is your model

$filteredUsers = $users->filter(function ($user) {
    return $user->role == 'admin'; // Filter users with role equal to 'admin'
});

// You can also use arrow functions in PHP 7.4+ for a more concise syntax
$filteredUsers = $users->filter(fn($user) => $user->role == 'admin');

// You can then iterate over the filtered collection to get the results
$filteredUsers->each(function ($user) {
    echo $user->name;
});


In this example, we used the filter() method to create a new collection with only the users that have a role equal to 'admin'. You can replace the condition in the callback function with any other condition you want to use for filtering the collection.


How to filter a Laravel collection by checking for null values?

You can filter a Laravel collection by checking for null values using the filter() method along with a callback function. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$collection = collect([
   ['name' => 'John', 'email' => 'john@example.com'],
   ['name' => 'Jane', 'email' => null],
   ['name' => 'Alex', 'email' => 'alex@example.com']
]);

$filteredCollection = $collection->filter(function ($item) {
    return !is_null($item['email']);
});

$filteredCollection->all();


In this example, the filter() method is used to iterate over each item in the collection and check if the email value is not null. If the value is not null, the item is included in the filtered collection. Finally, the all() method is used to retrieve the filtered items as an array.


After running this code, the $filteredCollection will only contain the items where the email value is not null.

Facebook Twitter LinkedIn Telegram

Related Posts:

You can use the getModel() method to return the class name of a collection instance in Laravel. For example: $collection = App\Models\User::all(); $className = $collection-&gt;getModel()-&gt;getKeyName(); In this example, $className will contain the class name...
To read data from a collection in Laravel, you can use the get() method on the collection object. This will retrieve all items from the collection and return them as an array. You can then iterate over the array to access individual items and their properties....
To sort values of an array in Laravel, you can use the built-in sortBy method provided by the Laravel Collection class. Simply pass the key of the array you want to sort as a parameter to the sortBy method. The method will return a new sorted collection based ...
To fetch data from a JSON array in Laravel, you can use the json_decode function to decode the JSON string into a PHP array. Once you have the array, you can access the data by using array access methods like foreach loop, array indexing, or other array functi...
To deploy Laravel on a Windows server, you will first need to have a Windows server environment set up with PHP and a web server such as Apache or Nginx installed. Next, you will need to download and install Composer, which is a dependency manager for PHP, and...