laravel-patterns
Laravel architecture patterns, routing/controllers, Eloquent ORM, service layers, queues, events, caching, and API resources for production apps. Use when building or reviewing Laravel apps — controllers, Eloquent, service layers, queues, or API resources.
By affaan-m · 3,127 installs
npx skills add affaan-m/ecc --skill laravel-patterns
Source repository · Upstream listing
Laravel Development Patterns
Production grade Laravel architecture patterns for scalable, maintainable applications.
When to Use
Building Laravel web applications or APIs
Structuring controllers, services, and domain logic
Working with Eloquent models and relationships
Designing APIs with resources and pagination
Adding queues, events, caching, and background jobs
How It Works
Structure the app around clear boundaries (controllers services/actions models).
Use explicit bindings and scoped bindings to keep routing predictable; still enforce authorization for access control.
Favor typed models, casts, and scopes to keep domain logic consistent.
Keep IO heavy work in queues and cache expensive reads.
Centralize config in config/ and keep environments explicit.
Examples
Project Structure
Use a conventional Laravel layout with clear layer boundaries (HTTP, services/actions, models).
Recommended Layout
Controllers Services Actions
Keep controllers thin. Put orchestration in services and single purpose logic in actions.
Routing and Controllers
Prefer route model binding and resource controllers for clarity.
Route Model Binding (Scoped)
Use scoped bindings to prevent cross tenant access.
Nested Routes and Binding Names
Keep prefixes and paths consistent to avoid double nesting (e.g., conversation vs conversations ).
Use a single parameter name that matches the bound model (e.g., {conversation} for Conversation ).
Prefer scoped bindings when nesting to enforce parent child relationships.
If you want a parameter to resolve to a different model class, define explicit binding. For custom binding logic, use Route::bind() or implement resolveRouteBinding() on the model.
Service Container Bindings
Bind interfaces to implementations in a service provider for clear dependency wiring.
Eloquent Model Patterns
Model Configuration
Custom Casts and Value Objects
Use enums or value objects for strict typing.
Eager Loading to Avoid N+1
Query Objects for Complex Filters
Global Scopes and Soft Deletes
Use global scopes for default filtering and SoftDeletes for recoverable records.
Use either a global scope or a named scope for the same filter, not both, unless you intend layered behavior.
Query Scopes for Reusable Filters
Transactions for Multi Step Updates
Migrations
Naming Convention
File names use timestamps: YYYY MM DD HHMMSS create users table.php
Migrations use anonymous classes (no named class); the filename communicates intent
Table names are snake case and plural by default
Example Migration
Form Requests and Validation
Keep validation in form requests and transform inputs to DTOs.
API Resources
Keep API responses consistent with resources and pagination.
Events, Jobs, and Queues
Emit domain events for side effects (emails, analytics)
Use queued jobs for slow work (reports, exports, webhooks)
Prefer idempotent handlers with retries and backoff
Caching
Cache read heavy endpoints and expensive queries
Invalidate caches on model events (created/updated/deleted)
Use tags when caching related data for easy invalidation
Configuration and Environments
Keep secrets in .env and config in config/ .php
Use per environment config overrides and config:cache in production