Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Specification Pattern

Laravel Specification Pattern Laravel Package

maartenpaauw/laravel-specification-pattern

A Laravel package for the specification pattern, letting you encapsulate business rules into reusable, composable specifications and apply them to Eloquent queries or collections for cleaner, testable domain logic.

View on GitHub
Deep Wiki
Context7

Filter an Illuminate collection with specifications.

Frequently asked questions about Laravel Specification Pattern
How do I install maartenpaauw/laravel-specification-pattern?
Install it with Composer using composer require maartenpaauw/laravel-specification-pattern. There is no service provider to register and no config file to publish, so integration is additive. Because the package only adds a behavioral layer over Illuminate collections, rolling it back is a simple composer remove with no schema, data, or state cleanup.
Which Laravel and PHP versions does this package support?
The package pins illuminate/* and PHP constraints, so check those against your target Laravel version and the next major you plan to adopt. A Laravel major bump is the most likely thing to strand a package this small. Run composer show maartenpaauw/laravel-specification-pattern to read the exact constraints before committing, and verify modern PHP is available in your runtime.
Does it require the companion specification-pattern package?
It most likely depends on maartenpaauw/specification-pattern for the core specification abstractions, with this package adding the collection integration. Check the dependency graph with composer show and confirm the companion library has compatible version pins and recent maintenance. If both are pinned to the same Laravel major, plan upgrades for them together.
Is it a replacement for Collection::filter() with a closure?
It solves the same problem but replaces anonymous closures with named, reusable, unit-testable objects. A closure works for a one-off case; a specification is worth the class when the same business rule is applied in several places or needs its own test. The trade-off is class sprawl, so adopt a naming convention and a review rule for composition early.
Can I use specifications to filter Eloquent queries instead of collections?
No. Specifications here evaluate already-materialized PHP objects, so they express business predicates rather than SQL and never touch indexes, query planning, or database-side pagination. The correct pattern is for a repository or query object to fetch a bounded candidate set or chunk, then apply specifications for eligibility, policy, or business-rule narrowing on top. Treating them as query filters leads to pulling broad result sets into memory, which becomes an incident at any real data volume.
How do I combine specifications with and, or, and not?
Composition is a core selling point: specifications are meant to be chained and combined so a rule like active-and-verified can be expressed from smaller reusable pieces. Check the README for the exact composition API before designing your specification library. Keep specifications as strict boolean predicates; if you find them sorting or transforming data, that is scope creep and belongs elsewhere.
What happens when a specification is applied to an empty collection?
In-memory filtering has silent semantics: a specification that returns an empty collection looks identical to no matches and to a bug. Test the empty-collection, null, and nested or associative collection cases explicitly in your own suite. Because there is no query planning to surface the difference, defensive tests are the only safety net.
How do I test specifications?
Specifications are plain objects, so you can assert each one individually with a plain PHPUnit test and no application boot, database, or framework container. Build a small fixture set of domain objects and test each spec plus your composed chains against it. Confirm the package's documented testing approach in the README, since it determines whether the collection extension itself needs a booted app.
Is it safe to use in production given the low star count?
The technical risk is low to moderate and is skewed toward longevity rather than runtime behavior: a single maintainer, low star count, and a recent 2026-03-23 release that reduces staleness risk. There are no external services, credentials, or schema changes, so the runtime blast radius is confined to the namespaces you point it at. The MIT license plus the small surface area make forking or vendoring a frozen copy a legitimate exit plan, and about one day of engineering confirms it is forkable in practice.
What are the alternatives to this package?
You can hand-roll specification classes and apply them with a custom collection macro or Collection::filter using dedicated predicate objects, which avoids any third-party dependency. Other specification-pattern packages for Laravel exist, but most take the same in-memory collection approach. The pattern itself matters more than the library, so pick whichever option keeps specifications plain, testable, and out of your query layer.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo