Laravel Collection Filter Method Example
Laravel Collection Filter Method Example

Laravel Collection Filter Method Example

Laravel is a popular PHP framework that simplifies the development of web applications. One of its powerful features is Collections, which provide an easy and elegant way to work with arrays of data. In this article, we’ll explore the filter method available in Laravel Collection Filter with practical examples to help you understand how to use it effectively.

What is the filter Method?

The filter method in Laravel Collections is used to filter elements of a collection using a callback function. This method removes elements that do not satisfy the given condition, returning a new collection with the remaining elements.

Basic Usage

To use the filter method, you need to create a collection first. You can do this using the collect helper function provided by Laravel.

Here’s a simple example to get you started:

use Illuminate\Support\Collection;

$collection = collect([1, 2, 3, 4, 5, 6]);

$filtered = $collection->filter(function ($value, $key) {
    return $value % 2 == 0;

dd($filtered); // Output: [2, 4, 6]

In this example, we created a collection of numbers and used the filter method to keep only the even numbers.

Filtering with Complex Conditions

You can also use more complex conditions within the callback function. Let’s say you have a collection of users, and you want to filter out users who are not active.

use Illuminate\Support\Collection;

$users = collect([
    ['name' => 'John', 'active' => true],
    ['name' => 'Jane', 'active' => false],
    ['name' => 'Doe', 'active' => true],

$activeUsers = $users->filter(function ($user) {
    return $user['active'];

// Output: [
//     ['name' => 'John', 'active' => true],
//     ['name' => 'Doe', 'active' => true],
// ]

In this example, the filter method keeps only the users who have the active attribute set to true.

Using Filter with Keys

The filter method also passes the key of each item to the callback function. This can be useful if you need to filter based on keys as well as values.

use Illuminate\Support\Collection;

$collection = collect([
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4,

$filtered = $collection->filter(function ($value, $key) {
    return $key == 'a' || $value > 2;

// Output: ['a' => 1, 'c' => 3, 'd' => 4]

In this example, we filtered the collection to keep the item with the key ‘a’ and items with values greater than 2.

Real-World Example: Filtering Products by Category

Consider a scenario where you have a list of products, and you want to filter them by category.

use Illuminate\Support\Collection;

$products = collect([
    ['name' => 'Laptop', 'category' => 'Electronics'],
    ['name' => 'Sofa', 'category' => 'Furniture'],
    ['name' => 'TV', 'category' => 'Electronics'],
    ['name' => 'Table', 'category' => 'Furniture'],

$electronics = $products->filter(function ($product) {
    return $product['category'] === 'Electronics';

// Output: [
//     ['name' => 'Laptop', 'category' => 'Electronics'],
//     ['name' => 'TV', 'category' => 'Electronics'],
// ]

In this example, we filtered the products collection to include only those products that belong to the ‘Electronics’ category.


The filter method in Laravel Collections is a powerful and flexible tool for working with arrays of data. By understanding how to use it with different types of conditions and data structures, you can easily filter your collections to meet specific criteria.

Remember, the filter method returns a new collection with the filtered results, leaving the original collection unchanged. This immutability makes it easy to chain multiple collection methods together in a clean and readable way.

Feel free to experiment with the filter method in your own projects to see how it can help simplify your data manipulation tasks!