Laravel best practices

From promovaweb/specsfy · View interactive version

Laravel best practices is an AI agent skill from the GitHub repository promovaweb/specsfy. Apply this skill whenever writing, reviewing, or refactoring Laravel PHP code. This includes creating or modifying controllers, models, migrations, form requests, policies, jobs, scheduled commands, service classes, and Eloquent queries. Triggers for N+1 and query performance issues, caching strateg

How to use this skill

Skill file

Laravel Best Practices

Best practices for Laravel, organized as an index of rule files. Each rule file teaches what to do and why. For exact API syntax, verify with search-docs.

Consistency First

Before applying any rule, check what the application already does. Laravel offers multiple valid approaches, and the best choice is the one the codebase already uses, even if another pattern would be theoretically better. Inconsistency is worse than a suboptimal pattern.

Check sibling files, related controllers, models, or tests for established patterns. If one exists, follow it. Don't introduce a second way. These rules are defaults for when no pattern exists yet, not overrides.

How to Apply

  1. Check the changed files, nearby code, project configuration, and relevant tests for established patterns. Deviate only for a correctness or security defect, and call the deviation out.
  2. Map every affected concern to the rule index below. Read each mapped rule file before editing. Skip unrelated rule files.
  3. Make the smallest coherent change. Keep the application's architecture and naming instead of introducing a second pattern for the same job.
  4. Verify version-sensitive Laravel APIs for the installed version with search-docs, or inspect the installed framework when it is unavailable.
  5. Run the narrowest relevant tests first, then the project's formatting and static-analysis checks when the change warrants them.
  6. Re-read the diff against every mapped rule before finishing.

Rule Index

Cross-cutting changes often need more than one rule file.

| Concern | Read |

| --- | --- |

| Query count, eager loading, indexes, large datasets | CODE0 |

| Subqueries, aggregates, complex ordering and query plans | CODE0 |

| Models, relationships, scopes, casts | CODE0 |

| Authentication, authorization, input safety, secrets, uploads | CODE0 |

| Form Requests and validation rules | CODE0 |

| Controllers, route binding, resources, middleware | CODE0 |

| Schema changes, columns, foreign keys, indexes | CODE0 |

| Jobs, retries, uniqueness, batches, Horizon | CODE0 |

| Cache lifetime, invalidation, locks, memoization | CODE0 |

| Outbound requests, retries, timeouts, fakes | CODE0 |

| Exceptions, reporting, rendering, log context | CODE0 |

| Events and notifications | CODE0 |

| Mailables and mail assertions | CODE0 |

| Scheduled tasks and overlap protection | CODE0 |

| Collections, lazy iteration, bulk operations | CODE0 |

| Blade components, attributes, composers | CODE0 |

| Environment values and application configuration | CODE0 |

| Pest/PHPUnit patterns, factories, fakes | CODE0 |

| Naming, helpers, file boundaries, PHP style | CODE0 |

| Actions, services, dependencies, application structure | CODE0 |

Decision Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.