Skip to content

Basic Examples โ€‹

Perfect for developers new to specifications

Start your specification journey with these complete walkthroughs. Each example shows the entire process from business logic recognition to testing, using common scenarios you'll encounter in real Laravel applications.

What You'll Learn โ€‹

  • ๐Ÿ” Business Logic Recognition - Spot scattered conditional logic
  • ๐ŸŽฏ Specification Strategy - Choose the right approach for your problem
  • โš’๏ธ Artisan Integration - Use make:specification command effectively
  • ๐Ÿงช Testing Setup - Unit tests with PHPUnit and Pest
  • ๐Ÿ”— Laravel Integration - Validation, events, and dependency injection

Blog Content Moderation System โ€‹

Time: 45 minutes | Integration: Validation, Events

Transform a messy content approval system into clean, testable specifications with complete Pest testing setup.

Perfect for learning:

  • Multi-step specification workflows
  • Event-driven architecture
  • Comprehensive test coverage
  • Form validation integration

User Account Validation โ€‹

Time: 30 minutes | Integration: Form Requests, Policies

Refactor user registration and profile update validation using specifications for clean, reusable business rules.

Perfect for learning:

  • Custom validation rules
  • Specification composition
  • Policy integration
  • Error handling

Email Campaign Targeting โ€‹

Time: 35 minutes | Integration: Collections, Jobs

Build a flexible email targeting system that segments users based on complex criteria using composable specifications.

Perfect for learning:

  • Collection filtering
  • Background job integration
  • Dynamic specification building
  • Performance optimization

Learning Path โ€‹

For Complete Beginners โ€‹

  1. Start here: Blog Content Moderation - Shows the complete workflow
  2. Build on it: User Account Validation - Adds validation patterns
  3. Expand further: Email Campaign Targeting - Introduces collections

For Laravel Developers โ€‹

If you're comfortable with Laravel but new to specifications:

  1. Skim Content Moderation for the pattern overview
  2. Focus on Account Validation for form integration
  3. Challenge yourself with Email Targeting for advanced patterns

Common Patterns You'll Master โ€‹

Specification Generation โ€‹

bash
# Basic specification
php artisan make:specification UserEligibilitySpecification

# With model context
php artisan make:specification ActiveUserSpecification --model=User

# Composite specification
php artisan make:specification ComplexValidationSpecification --composite

Testing Integration โ€‹

php
// PHPUnit style
public function test_specification_validates_correctly(): void
{
    $spec = new MySpecification();
    $this->assertTrue($spec->isSatisfiedBy($candidate));
}

// Pest style  
it('validates specification correctly', function () {
    $spec = new MySpecification();
    expect($spec->isSatisfiedBy($candidate))->toBeTrue();
});

Laravel Integration โ€‹

php
// Custom validation rule
$rules = [
    'user' => ['required', new SpecificationRule(new EligibleUserSpec())]
];

// Policy integration
public function update(User $user, Post $post): bool
{
    return $this->editableContentSpec->isSatisfiedBy($post);
}

Prerequisites โ€‹

  • Laravel 9+ application
  • Laravel Specifications package installed
  • Basic understanding of Laravel concepts (models, controllers, validation)
  • PHPUnit or Pest for testing

Ready to Start? โ€‹

Choose your first example and begin transforming scattered business logic into clean, maintainable specifications!

Start with Content Moderation โ†’

Released under the MIT License.