Quick Start โ
Get up and running with Laravel Specifications in minutes. This section provides the fastest path to implementing clean, testable business logic in your Laravel applications.
What You'll Learn โ
In the next 15 minutes, you'll:
- โ Install the package
- โ Create your first specification
- โ Use the powerful Artisan generator
- โ Transform messy controller logic into clean, reusable specifications
The Journey โ
1. Installation โ
Get Laravel Specifications installed and configured in under 2 minutes.
composer require dangerwayne/laravel-specifications
2. Your First Specification โ
Write your first specification and see the immediate benefits:
// Before: Messy controller logic
public function eligibleUsers()
{
return User::where('status', 'active')
->whereNotNull('email_verified_at')
->where('age', '>=', 18)
->where('subscription_type', '!=', 'basic')
->get();
}
// After: Clean, testable specification
$eligibleSpec = new EligibleUserSpecification();
$users = User::whereSpecification($eligibleSpec)->get();
3. Artisan Generator โ
Discover the powerful code generator that creates specifications instantly:
# Generate a specification with one command
php artisan make:specification User/ActiveUserSpecification --model=User --test
Why Specifications? โ
The Problem
Laravel controllers and models quickly become cluttered with complex business rules, making code hard to read, test, and maintain.
The Solution
Specifications encapsulate business rules into reusable, testable, and composable objects that make your code cleaner and more maintainable.
Real-World Preview โ
Here's what you'll be able to do after completing this quick start:
// Complex business logic made simple
$eligibleForDiscount = Specification::create()
->where('status', 'active')
->whereHas('orders', new RecentOrderSpecification())
->whereNotIn('country', ['US', 'CA']) // International customers
->whereBetween('total_spent', 100, 1000)
->build();
$discountEligibleUsers = User::whereSpecification($eligibleForDiscount)->get();
Key Benefits You'll Experience โ
๐งช Testable โ
$spec = new PremiumUserSpecification();
$this->assertTrue($spec->isSatisfiedBy($premiumUser));
$this->assertFalse($spec->isSatisfiedBy($basicUser));
๐ Reusable โ
// Use the same specification anywhere
$premiumSpec = new PremiumUserSpecification();
// In controllers
$users = User::whereSpecification($premiumSpec)->get();
// In collections
$premiumUsers = $allUsers->filter(fn($user) => $premiumSpec->isSatisfiedBy($user));
// In policies
public function viewPremiumContent(User $user): bool
{
return $premiumSpec->isSatisfiedBy($user);
}
๐ฏ Composable โ
$targetAudience = $activeSpec
->and($verifiedSpec)
->and($premiumSpec->or($loyalCustomerSpec));
Time Investment โ
- Installation: 2 minutes
- First Specification: 5 minutes
- Artisan Generator: 3 minutes
- Understanding the patterns: 5 minutes
Total: ~15 minutes to transform your Laravel development experience.
What's Next? โ
After completing the Quick Start:
- ๐ฝ๏ธ Appetizers - See dramatic before/after transformations
- ๐ Complete Guide - Master all features and patterns
- ๐๏ธ API Reference - Comprehensive documentation
- ๐ก Examples - Real-world implementations
Ready to Start?
Click Installation below to begin your journey to cleaner Laravel code!