API Reference
Complete API documentation for the Laravel Specifications package. This reference covers all interfaces, classes, and methods available in the package.
Core Interfaces
SpecificationInterface
The fundamental contract that all specifications must implement. Defines methods for evaluating candidates and building queries.
Base Classes
AbstractSpecification
The base implementation providing composite operations (AND, OR, NOT) and caching support.
Built-in Specifications
Common Specifications
- WhereSpecification - Basic WHERE clause conditions
- WhereInSpecification - WHERE IN conditions
- WhereBetweenSpecification - BETWEEN range conditions
- WhereNullSpecification - NULL/NOT NULL checks
- WhereHasSpecification - Relationship existence checks
Composite Specifications
- AndSpecification - Combine specifications with AND logic
- OrSpecification - Combine specifications with OR logic
- NotSpecification - Negate a specification
Builders & Facades
SpecificationBuilder
Fluent interface for building complex specifications with method chaining.
Specification Facade
Laravel facade providing convenient access to the builder.
Artisan Commands
make:specification
Generate specification classes with various templates and options.
Traits
CacheableSpecification
Add caching capabilities to your specifications.
Quick Reference
Creating a Specification
php
use DangerWayne\Specification\Specifications\AbstractSpecification;
class ActiveUserSpecification extends AbstractSpecification
{
public function isSatisfiedBy(mixed $candidate): bool
{
return $candidate->status === 'active';
}
public function toQuery(Builder $query): Builder
{
return $query->where('status', 'active');
}
}
Using the Fluent Builder
php
use DangerWayne\Specification\Facades\Specification;
$spec = Specification::create()
->where('status', 'active')
->whereNotNull('email_verified_at')
->whereBetween('age', 18, 65)
->build();
$users = User::whereSpecification($spec)->get();
Combining Specifications
php
$activeSpec = new ActiveUserSpecification();
$premiumSpec = new PremiumUserSpecification();
// AND combination
$activePremium = $activeSpec->and($premiumSpec);
// OR combination
$activeOrPremium = $activeSpec->or($premiumSpec);
// NOT operation
$notActive = $activeSpec->not();
Generating Specifications
bash
# Basic specification
php artisan make:specification UserActiveSpecification
# With model binding
php artisan make:specification UserPremiumSpecification --model=User
# Composite with caching
php artisan make:specification ComplexFilterSpecification --composite --cacheable
Method Index
SpecificationInterface Methods
isSatisfiedBy()
- Evaluate a candidatetoQuery()
- Apply to query builderand()
- AND combinationor()
- OR combinationnot()
- NegationgetCacheKey()
- Cache key generation
SpecificationBuilder Methods
where()
- Add WHERE conditionwhereIn()
- Add WHERE IN conditionwhereBetween()
- Add BETWEEN conditionwhereNull()
- Add NULL checkwhereNotNull()
- Add NOT NULL checkwhereHas()
- Add relationship checkbuild()
- Build the specification
Type Reference
Parameter Types
mixed $candidate
- The object or value to evaluateBuilder $query
- Eloquent query builder instanceSpecificationInterface $specification
- Specification instancestring $field
- Database column or field namearray $values
- Array of values for IN conditions
Return Types
bool
- Boolean result fromisSatisfiedBy()
Builder
- Modified query builder fromtoQuery()
SpecificationInterface
- New specification from composite operationsstring
- Cache key fromgetCacheKey()