Laravel Best Practices
Published by paulofelipem in agent-laravel-skills
What this skill does
Laravel best practices and architecture patterns for building production-ready applications. Use when writing, reviewing, or refactoring Laravel code to ensure proper patterns for service providers, dependency injection, security, Eloquent, and performance.
Add Laravel Best Practices to your agent
Review the source and files first. When you are ready, copy the prompt instruction or use the CLI command supported by your environment.
Install with a prompt
Paste this into a compatible coding agent:
add this skill "laravel-best-practices" from https://github.com/paulofelipem/agent-laravel-skillsInstall with the CLI
Run this command in a controlled environment after reviewing the repository:
npx skills add https://github.com/paulofelipem/agent-laravel-skills --skill laravel-best-practicesSkill instructions
Laravel Best Practices
Comprehensive best practices guide for Laravel applications. Contains 40 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Writing new Laravel controllers, services, or models
- Implementing authentication and authorization
- Reviewing code for architecture and security issues
- Refactoring existing Laravel codebases
- Optimizing performance or database queries
- Building microservices or queue-based architectures
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Architecture | CRITICAL | arch- |
| 2 | Dependency Injection | CRITICAL | di- |
| 3 | Error Handling | HIGH | error- |
| 4 | Security | HIGH | security- |
| 5 | Performance | HIGH | perf- |
| 6 | Testing | MEDIUM-HIGH | test- |
| 7 | Database & ORM | MEDIUM-HIGH | db- |
| 8 | API Design | MEDIUM | api- |
| 9 | Microservices | MEDIUM | micro- |
| 10 | DevOps & Deployment | LOW-MEDIUM | devops- |
Quick Reference
1. Architecture (CRITICAL)
arch-avoid-circular-deps- Avoid circular service provider dependenciesarch-feature-modules- Organize by domain/feature, not technical layerarch-single-responsibility- Focused services over "god services"arch-use-repository-pattern- Abstract database logic for testabilityarch-use-events- Event-driven architecture for decoupling
2. Dependency Injection (CRITICAL)
di-avoid-service-locator- Avoid app() helper as service locatordi-interface-segregation- Interface Segregation Principle (ISP)di-liskov-substitution- Liskov Substitution Principle (LSP)di-prefer-constructor-injection- Constructor over facade/helper injectiondi-scope-awareness- Understand singleton/bind/scoped bindingsdi-use-interfaces-tokens- Bind interfaces to implementations
3. Error Handling (HIGH)
error-use-exception-handler- Centralized exception handlingerror-throw-http-exceptions- Use proper HTTP exceptionserror-handle-queue-errors- Handle queue and job errors properly
4. Security (HIGH)
security-auth-jwt- Secure authentication with Sanctum/Passportsecurity-validate-all-input- Validate with Form Requestssecurity-use-guards- Authentication guards, gates, and policiessecurity-sanitize-output- Prevent XSS attackssecurity-rate-limiting- Implement rate limiting
5. Performance (HIGH)
perf-service-provider-lifecycle- Proper service provider boot/register lifecycleperf-use-caching- Implement caching strategiesperf-optimize-database- Optimize database queriesperf-lazy-loading- Lazy collections and route caching
6. Testing (MEDIUM-HIGH)
test-use-testcase-refresh-database- Use Laravel TestCase and RefreshDatabasetest-e2e-http- HTTP/feature testing with Laraveltest-mock-external-services- Mock external dependencies with facades
7. Database & ORM (MEDIUM-HIGH)
db-use-transactions- Transaction managementdb-avoid-n-plus-one- Avoid N+1 query problems with eager loadingdb-use-migrations- Use migrations for schema changes
8. API Design (MEDIUM)
api-use-dto-serialization- API Resources and serializationapi-use-interceptors- Middleware for cross-cutting concernsapi-versioning- API versioning strategiesapi-use-pipes- Form Requests for input transformation
9. Microservices (MEDIUM)
micro-use-patterns- Message and event patternsmicro-use-health-checks- Health checks for orchestrationmicro-use-queues- Background job processing with queues
10. DevOps & Deployment (LOW-MEDIUM)
devops-use-config-module- Environment configuration with config()devops-use-logging- Structured logging with channelsdevops-graceful-shutdown- Zero-downtime deployments
How to Use
Read individual rule files for detailed explanations and code examples:
rules/arch-avoid-circular-deps.md
rules/security-validate-all-input.md
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Only read
AGENTS.mdor individualrules/*.mdfiles when you need full implementation detail for a specific rule. Do not load them proactively.
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
Laravel Boost Compatibility
If Laravel Boost is installed, the following rules are already covered by its core guidelines and can be skipped:
security-validate-all-input, security-auth-jwt, db-avoid-n-plus-one,
db-use-migrations, perf-use-caching, test-use-testcase-refresh-database,
devops-use-logging, api-versioning
Files included
- SKILL.md
- .gitignore
- metadata.json
- rules/_template.md
- rules/api-use-dto-serialization.md
- rules/api-use-pipes.md
- rules/api-use-interceptors.md
- rules/api-versioning.md
- README.md
- rules/arch-single-responsibility.md
- rules/arch-avoid-circular-deps.md
- rules/arch-use-events.md
- rules/db-use-transactions.md
- rules/arch-feature-modules.md
- rules/db-avoid-n-plus-one.md
- rules/arch-use-repository-pattern.md
- rules/db-use-migrations.md
- rules/devops-graceful-shutdown.md
- AGENTS.md
- rules/devops-use-config-module.md
- rules/devops-use-logging.md
- rules/error-handle-queue-errors.md
- rules/di-prefer-constructor-injection.md
- rules/di-interface-segregation.md
- rules/di-liskov-substitution.md
- rules/di-use-interfaces-tokens.md
- rules/di-avoid-service-locator.md
- rules/di-scope-awareness.md
- rules/error-throw-http-exceptions.md
- rules/micro-use-health-checks.md
- rules/error-use-exception-handler.md
- rules/micro-use-queues.md
- rules/perf-optimize-database.md
- rules/micro-use-patterns.md
- rules/perf-service-provider-lifecycle.md
- rules/perf-lazy-loading.md
- rules/perf-use-caching.md
- rules/security-rate-limiting.md
- rules/security-auth-jwt.md
- rules/security-use-guards.md
- rules/security-sanitize-output.md
- rules/security-validate-all-input.md
- rules/test-e2e-http.md
- rules/test-mock-external-services.md
- rules/test-use-testcase-refresh-database.md
- scripts/build-agents.ts
- scripts/build.sh
- scripts/package-lock.json
- scripts/package.json

