Skip to content

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.

bash
composer require dangerwayne/laravel-specifications

2. Your First Specification โ€‹

Write your first specification and see the immediate benefits:

php
// 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:

bash
# 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:

php
// 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 โ€‹

php
$spec = new PremiumUserSpecification();
$this->assertTrue($spec->isSatisfiedBy($premiumUser));
$this->assertFalse($spec->isSatisfiedBy($basicUser));

๐Ÿ”„ Reusable โ€‹

php
// 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 โ€‹

php
$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!

Get Started โ†’

Released under the MIT License.