laravel-best-practices

Applies Laravel best practices when writing, reviewing, and refactoring PHP backend code.

1|Updated Jun 6, 2026
One-click install
npx skills add https://github.com/karuhun-developer/ecommerce --skill laravel-best-practices-karuhun-developer
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: laravel-best-practices
Source: https://github.com/karuhun-developer/ecommerce/tree/main/.cursor/skills/laravel-best-practices
Command: npx skills add https://github.com/karuhun-developer/ecommerce --skill laravel-best-practices-karuhun-developer

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Laravel offers multiple valid ways to solve the same problem, which leads to inconsistent codebases, N+1 query bugs, security gaps, and performance regressions. This Skill provides a prioritized, opinionated rule set so every controller, model, migration, and job follows proven Laravel conventions. ## Core Features & Use Cases - 19 Rule Categories: Covers database performance, advanced queries, security, caching, Eloquent patterns, validation, configuration, testing, queues, routing, HTTP client, events, mail, error handling, scheduling, architecture, migrations, collections, Blade views, and code style. - Incorrect vs. Correct Examples: Each rule shows anti-patterns alongside the recommended implementation with runnable PHP code snippets. - Consistency-First Guidance: Instructs checking sibling files for existing project patterns before applying any default rule. - Use Case: When refactoring a slow product listing endpoint, the Skill directs you to eager loading with with(), withCount() for relation counts, column selection, and proper indexing to eliminate N+1 queries. ## Quick Start Review my Laravel OrderController and Eloquent queries for N+1 problems, missing authorization, and validation issues using the Laravel best practices skill.

Frequently Asked Questions about laravel-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 query problems in Laravel Eloquent?▼

Fix N+1 queries by eager loading relationships with `with()` so related models load in one query instead of per-iteration. Enable `Model::preventLazyLoading()` in development to catch violations, and use `withCount()` instead of loading collections just to count them.

How to validate requests in Laravel controllers?▼

Extract validation into dedicated Form Request classes and type-hint them in controller methods for automatic validation. Always use `$request->validated()` for mass operations instead of `$request->all()`, and prefer array notation for rules in new code.

What caching strategy should I use in Laravel?▼

Use `Cache::remember()` for the cache-aside pattern, `Cache::flexible()` for stale-while-revalidate on high-traffic keys, and `Cache::memo()` to avoid redundant hits within a request. Add cache tags for group invalidation and configure failover stores in production.

Does this Laravel skill work with older framework versions?▼

The rules target modern Laravel features like `Cache::flexible()`, `defer()`, and `Context`, so some guidance requires recent versions. The Skill instructs verifying exact API syntax with `search-docs` against your installed Laravel version before applying rules.

Why are my Laravel queued jobs running twice?▼

Duplicate job execution happens when `retry_after` in queue config is shorter than the job's `timeout`, causing re-dispatch while still running. Set `retry_after` greater than the timeout and implement `ShouldBeUnique` to prevent duplicate processing.

When should I not modify a Laravel migration?▼

Never modify a migration that has already run in production; treat it as immutable and create a new migration to alter the table. Keep one concern per migration and never mix schema changes with data manipulation.