maartenpaauw/laravel-specification-pattern

Filter an Illuminate collection with specifications
39,544 15
Install
composer require maartenpaauw/laravel-specification-pattern
Latest Version:v3.1.0
PHP:^8.2
Laravel:12 · 13
Older releases:8 – 11
License:MIT
Last Updated:Jul 9, 2026
Links: GitHub  ·  Packagist
Maintainer: maartenpaauw

Laravel specification pattern

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Codecov Total Downloads

Filter an Illuminate collection with specifications.

Support me

You can support me by buying Pennant feature flags for Filament.

Installation

You can install the package via composer:

composer require maartenpaauw/laravel-specification-pattern

You can publish the config file with:

php artisan vendor:publish --tag="laravel-specification-pattern-config"

This is the contents of the published config file:

return [
    'collection-method' => 'matching',
];

Usage

Here's how you can create a specification:

php artisan make:specification AdultSpecification

This will generate a specification class within the App\Specifications namespace.

<?php

namespace App\Specifications;

use Maartenpaauw\Specifications\Specification;

/**
 * @implements  Specification<mixed>
 */
class AdultSpecification implements Specification
{
    /**
     * {@inheritDoc}
     */
    public function isSatisfiedBy(mixed $candidate): bool
    {
        return true;
    }
}

Imagine we have the following class which represents a person with a given age.

class Person {
    public function __construct(public int $age)
    {}
}

Let's apply the business logic:

<?php

namespace App\Specifications;

use Maartenpaauw\Specifications\Specification;

/**
- * @implements  Specification<mixed>
+ * @implements  Specification<Person>
 */
class AdultSpecification implements Specification
{
    /**
     * {@inheritDoc}
     */
    public function isSatisfiedBy(mixed $candidate): bool
    {
-        return true;
+        return $candidate->age >= 18;
    }
}

After applying the bussiness logic we can use the specification by directly calling the isSatisfiedBy method or indirectly be filtering an eloquent collection by calling the matching method.

Direct

$specification = new AdultSpecification();

// ...

$specification->isSatisfiedBy(new Person(16)); // false
$specification->isSatisfiedBy(new Person(32)); // true

Indirect

$persons = collect([
    new Person(10),
    new Person(17),
    new Person(18),
    new Person(32),
]);

// ...

// Returns a collection with persons matching the specification
$persons = $persons->matching(new AdultSpecification());

// ...

$persons->count(); // 2

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

maartenpaauw/filament-cashier-billing-provider

Filament billing provider for Laravel Cashier

47,071 104
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

644,549,754 9,698
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

507,193,856 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

423,477,751 607

Version History

Version Released PHP Laravel License
v3.1.0 ^8.2 ^12.0|^13.0 MIT
v3.0.1 ^8.2 ^11.0|^12.0 MIT
v3.0.0 ^8.2 ^11.0|^12.0 MIT
v2.8.0 ^8.1 ^10.0|^11.0 MIT
v2.7.0 ^8.1 ^10.0|^11.0 MIT