1---2name: backend-laravel3description: Build, maintain, and debug Laravel backends (API and web).4---56# Backend - Laravel78## When to use this skill9- Creating APIs, controllers, routes, form requests, models, and migrations.10- Implementing authentication/authorization, validation, queues, notifications, or mail.11- Debugging performance, database queries, or request/response handling.1213## Quick start141. Install PHP deps: `composer install`.152. Env: copy `.env.example` -> `.env`; set `APP_KEY` via `php artisan key:generate`; configure DB and queue.163. Run migrations/seeders: `php artisan migrate --seed` (if seeders exist).174. Serve: `php artisan serve` or via Sail/Valet; tests: `php artisan test`; quality: `composer lint` if defined.1819## Project structure basics20- Routes: `routes/api.php` for stateless APIs; `routes/web.php` for web routes with sessions.21- Controllers: thin; push business logic into services/actions; type-hint dependencies for auto DI.22- Form Requests: validate/authorize input; keep controllers lean.23- Models and Eloquent: define fillable/guarded; relationships and scopes for query reuse.24- Migrations/Seeders: keep schema in migrations; seed minimal fixtures for dev/test.2526## Coding principles27- Keep controllers thin; validate/authorize via Form Requests; push logic to services/actions.28- Normalize errors in exception handler; avoid leaking internals; include request IDs in logs.29- Secure by default: policies/gates for authz, hashed secrets, avoid logging sensitive data.30- Handle migrations carefully; prefer reversible changes and minimal seeds for tests.31- Enforce tests and available lint/static tools before merge (run `scripts/dev-check.sh`).3233## Patterns and snippets34- API route, Form Request, controller, queue, and caching templates live in references/snippets.md.35- Use resources/transformers to keep API shapes consistent; eager-load to avoid N+1.3637## Security and auth38- Use Laravel Sanctum/Passport for API auth; hash secrets; never log credentials.39- Authorize actions via policies or gates; ensure middleware protects routes.4041## Error handling and logging42- Customize exception rendering in `app/Exceptions/Handler.php`; map validation and domain errors clearly.43- Use structured logs; include request IDs for tracing.4445## Testing46- Use Pest or PHPUnit; feature tests for HTTP flows, unit tests for services.47- Refresh DB between tests (`RefreshDatabase` trait); seed only necessary data.4849## Bundled resources50- scripts/dev-check.sh: run before commit/PR; installs Composer deps if needed, then runs tests and any defined lint/pint/phpstan scripts to catch issues early.51- references/coding-standards.md: quick guardrails for controllers/services, validation, Eloquent usage, error handling, and security.52- references/best-practices.md: deeper guidance for routing, controllers, validation, Eloquent, caching, security, and testing.53- references/snippets.md: templates for routes, Form Requests, controllers, queues, and caching patterns.54- assets/pr-template.md: PR checklist for tests, migrations, and API/resource consistency.55- assets/migration-checklist.md: use when adding schema changes to ensure safety and communication.5657## Delivery checklist58- Migrations up; env configured; queues and caches warmed.59- Tests and lints pass; no debug code; errors mapped to API-friendly JSON.60- Resources/transformers return consistent shapes; pagination and sorting documented.