$npx -y skills add gdarko/laravel-vue-starter --skill laravel-best-practicesApply 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 iss
| 1 | # Laravel Best Practices |
| 2 | |
| 3 | Best practices for Laravel, prioritized by impact. Each rule teaches what to do and why. For exact API syntax, verify with `search-docs`. |
| 4 | |
| 5 | ## Consistency First |
| 6 | |
| 7 | Before applying any rule, check what the application already does. Laravel offers multiple valid approaches — 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. |
| 8 | |
| 9 | 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. |
| 10 | |
| 11 | ## Quick Reference |
| 12 | |
| 13 | ### 1. Database Performance → `rules/db-performance.md` |
| 14 | |
| 15 | - Eager load with `with()` to prevent N+1 queries |
| 16 | - Enable `Model::preventLazyLoading()` in development |
| 17 | - Select only needed columns, avoid `SELECT *` |
| 18 | - `chunk()` / `chunkById()` for large datasets |
| 19 | - Index columns used in `WHERE`, `ORDER BY`, `JOIN` |
| 20 | - `withCount()` instead of loading relations to count |
| 21 | - `cursor()` for memory-efficient read-only iteration |
| 22 | - Never query in Blade templates |
| 23 | |
| 24 | ### 2. Advanced Query Patterns → `rules/advanced-queries.md` |
| 25 | |
| 26 | - `addSelect()` subqueries over eager-loading entire has-many for a single value |
| 27 | - Dynamic relationships via subquery FK + `belongsTo` |
| 28 | - Conditional aggregates (`CASE WHEN` in `selectRaw`) over multiple count queries |
| 29 | - `setRelation()` to prevent circular N+1 queries |
| 30 | - `whereIn` + `pluck()` over `whereHas` for better index usage |
| 31 | - Two simple queries can beat one complex query |
| 32 | - Compound indexes matching `orderBy` column order |
| 33 | - Correlated subqueries in `orderBy` for has-many sorting (avoid joins) |
| 34 | |
| 35 | ### 3. Security → `rules/security.md` |
| 36 | |
| 37 | - Define `$fillable` or `$guarded` on every model, authorize every action via policies or gates |
| 38 | - No raw SQL with user input — use Eloquent or query builder |
| 39 | - `{{ }}` for output escaping, `@csrf` on all POST/PUT/DELETE forms, `throttle` on auth and API routes |
| 40 | - Validate MIME type, extension, and size for file uploads |
| 41 | - Never commit `.env`, use `config()` for secrets, `encrypted` cast for sensitive DB fields |
| 42 | |
| 43 | ### 4. Caching → `rules/caching.md` |
| 44 | |
| 45 | - `Cache::remember()` over manual get/put |
| 46 | - `Cache::flexible()` for stale-while-revalidate on high-traffic data |
| 47 | - `Cache::memo()` to avoid redundant cache hits within a request |
| 48 | - Cache tags to invalidate related groups |
| 49 | - `Cache::add()` for atomic conditional writes |
| 50 | - `once()` to memoize per-request or per-object lifetime |
| 51 | - `Cache::lock()` / `lockForUpdate()` for race conditions |
| 52 | - Failover cache stores in production |
| 53 | |
| 54 | ### 5. Eloquent Patterns → `rules/eloquent.md` |
| 55 | |
| 56 | - Correct relationship types with return type hints |
| 57 | - Local scopes for reusable query constraints |
| 58 | - Global scopes sparingly — document their existence |
| 59 | - Attribute casts in the `casts()` method |
| 60 | - Cast date columns, use Carbon instances in templates |
| 61 | - `whereBelongsTo($model)` for cleaner queries |
| 62 | - Never hardcode table names — use `(new Model)->getTable()` or Eloquent queries |
| 63 | |
| 64 | ### 6. Validation & Forms → `rules/validation.md` |
| 65 | |
| 66 | - Form Request classes, not inline validation |
| 67 | - Array notation `['required', 'email']` for new code; follow existing convention |
| 68 | - `$request->validated()` only — never `$request->all()` |
| 69 | - `Rule::when()` for conditional validation |
| 70 | - `after()` instead of `withValidator()` |
| 71 | |
| 72 | ### 7. Configuration → `rules/config.md` |
| 73 | |
| 74 | - `env()` only inside config files |
| 75 | - `App::environment()` or `app()->isProduction()` |
| 76 | - Config, lang files, and constants over hardcoded text |
| 77 | |
| 78 | ### 8. Testing Patterns → `rules/testing.md` |
| 79 | |
| 80 | - `LazilyRefreshDatabase` over `RefreshDatabase` for speed |
| 81 | - `assertModelExists()` over raw `assertDatabaseHas()` |
| 82 | - Factory states and sequences over manual overrides |
| 83 | - Use fakes (`Event::fake()`, `Exceptions::fake()`, etc.) — but always after factory setup, not before |
| 84 | - `recycle()` to share relationship instances across factories |
| 85 | |
| 86 | ### 9. Queue & Job Patterns → `rules/queue-jobs.md` |
| 87 | |
| 88 | - `retry_after` must exceed job `timeout`; use exponential backoff `[1, 5, 10]` |
| 89 | - `ShouldBeUnique` to prevent duplicates; `WithoutOverlapping::untilProcessing()` for concurrency |
| 90 | - Always implement `failed()`; with `retryUntil()`, set `$tries = 0` |
| 91 | - `RateLimited` middleware for external API calls; `Bus::batch()` for relate |