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
Featured Examples โ
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 โ
- Start here: Blog Content Moderation - Shows the complete workflow
- Build on it: User Account Validation - Adds validation patterns
- Expand further: Email Campaign Targeting - Introduces collections
For Laravel Developers โ
If you're comfortable with Laravel but new to specifications:
- Skim Content Moderation for the pattern overview
- Focus on Account Validation for form integration
- Challenge yourself with Email Targeting for advanced patterns
Common Patterns You'll Master โ
Specification Generation โ
# 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 โ
// 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 โ
// 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!