laravel-best-practices

Applies Laravel PHP best-practice rules when writing, reviewing, or refactoring backend code.

1|Updated Aug 4, 2026
One-click install
npx skills add https://github.com/LaravelDaily/nativephp-larapackagehunt --skill laravel-best-practices-laraveldaily
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel-best-practices
Source: https://github.com/LaravelDaily/nativephp-larapackagehunt/tree/main/.agents/skills/laravel-best-practices
Command: npx skills add https://github.com/LaravelDaily/nativephp-larapackagehunt --skill laravel-best-practices-laraveldaily

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 an indexed rulebook of Laravel conventions so code is written and reviewed against consistent, proven patterns. ## Core Features & Use Cases - Indexed Rule Files: Twenty topic-specific rule files covering Eloquent, migrations, validation, routing, queues, caching, security, testing, Blade, HTTP client, scheduling, and architecture. - Consistency-First Guidance: Instructs checking existing codebase patterns before applying any rule, so new code matches established conventions instead of introducing a second approach. - Incorrect/Correct Code Examples: Each rule shows concrete PHP snippets of what to avoid and what to write instead, covering issues like N+1 queries, mass assignment, missing indexes, and unqueued notifications. - Use Case: When refactoring a slow Laravel controller, map the concerns to the rule index, read the db-performance and routing rule files, then apply eager loading, column selection, and thin-controller patterns. ## Quick Start Ask the AI to review your Laravel controller, model, or migration against the Laravel best practices rules and suggest fixes.

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?

Fix N+1 queries in Laravel by eager loading relationships with with() before iterating, and use withCount() instead of loading collections just to count them. Enable Model::preventLazyLoading() in AppServiceProvider during development to catch violations automatically.

How to validate requests in Laravel controllers?

Validate requests in Laravel by type-hinting a dedicated Form Request class in the controller method, which runs validation and authorization automatically. Then use $request->validated() to retrieve only validated data instead of $request->all().

Should I use whereHas or whereIn for Laravel relationship filtering?

Prefer whereIn with a select('id') subquery over whereHas for relationship filtering, because whereHas emits a correlated EXISTS subquery that re-executes per row. The whereIn approach lets the database use an index lookup without loading data into PHP memory.

When should Laravel notifications and mailables be queued?

Queue notifications and mailables whenever they hit external services like email, SMS, or Slack, since sending synchronously blocks the HTTP response. Implement ShouldQueue on the class and use afterCommit() when dispatching inside database transactions.

Why does my Laravel queued job run twice?

Duplicate job execution happens when the queue's retry_after value is shorter than the job's timeout, causing the worker to re-dispatch a still-running job. Set retry_after greater than the longest job timeout and implement ShouldBeUnique to prevent duplicates.

Can I modify a Laravel migration after it runs in production?

No, deployed migrations should be treated as immutable. Create a new migration to alter the table instead, and write reversible down() methods so migrate:rollback works in CI and failed deployments.