Horje
How to Filter a Collection in Laravel?

In Laravel, filtering a collection involves selecting specific items based on set conditions, offering flexibility and control over data. This process helps refine collections to meet specific criteria for enhanced data management.

Below are the methods to filter a collection in Laravel:

Using Filter() Function

The filter method in Laravel allows us to filter a collection using a callback function. This callback function receives each item in the collection as its argument and should return true if the item should be included in the filtered result or false otherwise.

Example: This example shows the filter method with a callback function.

PHP
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

// Filter even numbers
$filtered = $collection->filter(function 
                                ($value, $key) {
    return $value % 2 === 0;
});

// Output the filtered collection
$filtered->each(function ($item) {
    echo $item . ' ';
});

Output:

Screenshot-2024-04-27-153459

Using Reduce() Method

The reduce method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration. the reduce method iterates through each item in the collection. If an item is even, add it to the $carry array, acting as an accumulator.

Example: This example shows the filter a collection with using the reduce method.

PHP
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

// Filter even numbers using reduce
$filteredArray = $collection->reduce(function
                                    ($carry, $item) {
    if ($item % 2 === 0) {
        $carry[] = $item;
    }
    return $carry;
}, []);

// Convert the filtered array back to a collection
$filtered = collect($filteredArray);

// Output the filtered collection using each
$filtered->each(function ($item) {
    echo $item . ' ';
});

Output:

Screenshot-2024-04-27-153459

Using Reject() Method

The reject method filters the collection using the given callback function . The function should return true if the item should be removed from the resulting collection. In our case we want to keep the even numbers in the collections and remove the odd numbers, so we will use the reject() method to remove the odd numbers from the collection.

Example: This example shows the filter a collection with using the reject() method.

PHP
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

// Filter even numbers
$filtered = $collection->reject(function 
                               ($value, $key) {
    return $value % 2 !== 0;
});

// Output the filtered collection
$filtered->each(function ($item) {
    echo $item . ' ';
});

Output:

Screenshot-2024-04-27-153459




Reffered: https://www.geeksforgeeks.org


PHP

Related
PHP Program to Convert a Given Number to Words PHP Program to Convert a Given Number to Words
PHP iconv_strlen() Function PHP iconv_strlen() Function
PHP Determinant of a Matrix PHP Determinant of a Matrix
How to Display Days in a Grid by Weeks in PHP? How to Display Days in a Grid by Weeks in PHP?
Remove First Element from an Array in PHP Remove First Element from an Array in PHP

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13