Download the PHP package avoqado-dev/laravel-usecase without Composer
On this page you can find all versions of the php package avoqado-dev/laravel-usecase. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download avoqado-dev/laravel-usecase
More information about avoqado-dev/laravel-usecase
Files in avoqado-dev/laravel-usecase
Package laravel-usecase
Short Description A lightweight, type-safe Use Case pattern implementation for Laravel with business rule validation
License MIT
Informations about the package laravel-usecase
๐ฏ Laravel UseCase
A lightweight, type-safe Use Case pattern implementation for Laravel with built-in business rule validation
๐ Table of Contents
- What is Laravel UseCase?
- Why Use This Package?
- Key Features
- Installation
- Quick Start
- Middleware Explained
- Business Rules
- Testing
- Configuration
- Support & Contribution
๐ฏ What is Laravel UseCase?
Laravel UseCase brings clean architecture principles to your Laravel applications by implementing the Use Case pattern (also known as Application Services or Command/Query pattern). It helps you organize your business logic into focused, testable, and reusable units that are completely independent of your HTTP layer.
Instead of fat controllers or bloated models, your business logic lives in dedicated Use Case classes that can be called from anywhere - controllers, jobs, commands, or even other use cases.
The Problem It Solves
๐ Why Use This Package?
โจ Benefits
| Benefit | Description |
|---|---|
| ๐ฏ Separation of Concerns | Business logic is completely isolated from HTTP, making it reusable across controllers, jobs, commands, and tests |
| ๐ Type Safety | Full PHP 8.2+ type hints and generics catch errors at compile time, not runtime |
| ๐งช Testability | Test business logic in isolation without booting the HTTP layer or database |
| ๐ฆ Maintainability | Clear, predictable structure makes onboarding new developers faster |
| โป๏ธ Reusability | Use cases can be called from anywhere - no duplication needed |
| ๐จ Business-First | Domain rules are first-class citizens with dedicated validation layer |
| ๐ง Laravel-Native | Uses familiar Laravel patterns - facades, container, middleware, events |
| ๐ Observability | Built-in logging, caching, and transaction management |
| โก Performance | Automatic caching and atomic locks prevent redundant work |
| ๐ก๏ธ Safety | Database transactions and retry logic built-in |
โจ Key Features
- โ Type-Safe - Uses PHP 8.2+ generics for return type safety
- โ Business Rule Validation - Separates domain rules from HTTP validation
- โ Middleware Pipeline - Familiar Laravel-style middleware for cross-cutting concerns
- โ Zero Dependencies - Only requires Laravel, no third-party packages
- โ Comprehensive Testing Utilities - Full mocking and assertion capabilities
- โ Artisan Generator - Scaffold use cases with a single command
- โ Built-in Middleware - Transactions, caching, locks, logging out of the box
- โ Laravel-Native Integration - Uses facades, container, events naturally
๐ฆ Installation
Install via Composer:
The package will auto-register via Laravel's package discovery.
Publish Configuration (Optional)
๐ Quick Start
1. Generate a Use Case
The easiest way to create a new use case is using the artisan command:
This generates both the Request and Handler classes in app/UseCases/{Module}/{Entity}/{UseCaseName}/.
2. Define Your Request
3. Implement Your Handler
4. Dispatch from Anywhere
๐ง Middleware Explained
Laravel UseCase includes powerful middleware to handle cross-cutting concerns automatically. Simply implement the corresponding interface on your Request class!
1. ๐ Database Transactions
What it does: Automatically wraps your use case in a database transaction with retry logic.
When to use: Any operation that modifies data and needs atomicity (create, update, delete operations).
Benefits:
- โ Automatic rollback on exceptions
- โ Configurable retry attempts for deadlocks
- โ No manual transaction management needed
Example Use Cases:
- Creating orders with multiple related records
- Updating inventory across multiple tables
- Processing payments with audit logs
2. ๐พ Caching
What it does: Automatically caches the result of your use case to avoid redundant computation.
When to use: Read-heavy operations with expensive computations or database queries.
Benefits:
- โ Automatic cache invalidation via TTL
- โ Configurable cache keys per request
- โ Reduces database load significantly
Example Use Cases:
- Dashboard statistics
- Report generation
- Complex aggregations
- API responses with heavy processing
3. ๐ Atomic Locks
What it does: Prevents concurrent execution of the same use case using Laravel's atomic locks.
When to use: Operations that must not run simultaneously (payment processing, inventory updates).
Benefits:
- โ Prevents race conditions
- โ Ensures data consistency
- โ Configurable wait time
Example Use Cases:
- Payment processing
- Inventory deduction
- Seat reservation systems
- Concurrent user actions on same resource
4. ๐ Read from Write Database
What it does: Forces read operations to use the write database connection (master) instead of read replicas.
When to use: When you need to read immediately after a write to avoid replication lag.
Benefits:
- โ Avoids stale data from read replicas
- โ Ensures read-after-write consistency
- โ No manual connection switching
Example Use Cases:
- Reading data immediately after creation
- Displaying confirmation pages
- Real-time updates
- Critical operations requiring latest data
5. ๐ Logger Middleware
What it does: Automatically logs use case execution (request received, success, failure).
When to use: Enabled by default for all use cases (can be disabled in config).
Benefits:
- โ Automatic audit trail
- โ Debug failed operations
- โ Monitor use case performance
- โ Configurable log channel
Log Output:
๐ฏ Combining Middleware
You can combine multiple middleware on a single use case:
Execution Order:
- Logger (request received)
- Read from Write Database
- Atomic Lock (acquire)
- Cache (check)
- Database Transaction (begin)
- Your Handler Logic
- Database Transaction (commit)
- Cache (store)
- Atomic Lock (release)
- Logger (success/failure)
๐จ Business Rules
Business rules represent domain-specific validation that goes beyond simple input validation. They encapsulate your business logic and can be tested independently.
Creating a Business Rule
Using Business Rules
Pattern Matching on Exceptions
๐งช Testing
The package provides comprehensive testing utilities for both use cases and business rules.
Testing Use Cases
Testing Business Rules
Testing Broken Rules
โ๏ธ Configuration
Customize the package behavior in config/usecase.php:
Middleware Order
Logging
Business Rule HTTP Status
๐ Support & Contribution
โญ Star This Repository
If you find this package useful, please consider giving it a star on GitHub! It helps others discover the package and motivates us to continue improving it.
๐ค Contributing
We welcome contributions! Whether it's:
- ๐ Bug reports
- ๐ก Feature requests
- ๐ Documentation improvements
- ๐ง Code contributions
Please see CONTRIBUTING.md for details on our code of conduct and development process.
๐ฃ Spread the Word
Help us grow the community:
- Share on Twitter/X
- Write a blog post about your experience
- Mention it in your Laravel projects
- Recommend it to your team
๐ฌ Get Help
- Issues: GitHub Issues
- Discussions: GitHub Discussions
๐ License
MIT License - see LICENSE file for details.
๐จโ๐ป Credits
Created and maintained by:
Made with โค๏ธ for the Laravel community
โญ Star us on GitHub โข ๐ Report Bug โข ๐ฌ Discussions
All versions of laravel-usecase with dependencies
illuminate/support Version ^12.0|^13.0
illuminate/container Version ^12.0|^13.0
illuminate/database Version ^12.0|^13.0
illuminate/cache Version ^12.0|^13.0