laravel-best-practices

Applies Laravel coding conventions and best practices when writing, reviewing, or refactoring PHP code.

Updated Aug 31, 2026
One-click install
npx skills add https://github.com/coleYab/usacr --skill laravel-best-practices-coleyab
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel-best-practices
Source: https://github.com/coleYab/usacr/tree/main/.grok/skills/laravel-best-practices
Command: npx skills add https://github.com/coleYab/usacr --skill laravel-best-practices-coleyab

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 unmaintainable controllers. This Skill provides an indexed rule set that guides AI-assisted writing, reviewing, and refactoring of Laravel PHP code toward consistent, framework-idiomatic patterns. ## Core Features & Use Cases - Indexed Rule Library: Twenty topic-specific rule files covering Eloquent, database performance, advanced queries, migrations, validation, routing, security, caching, queues, scheduling, mail, events, HTTP client, error handling, collections, Blade views, configuration, architecture, and style. - Consistency-First Guidance: Instructs checking existing project patterns before applying any rule, so refactors match the codebase instead of introducing competing conventions. - Concrete Code Examples: Each rule pairs incorrect and correct PHP snippets, covering issues like N+1 prevention with eager loading, atomic locks with lockForUpdate, mass-assignment protection, and queue retry configuration. - Use Case: When asked to refactor a slow Laravel admin panel, the Skill maps the concern to the db-performance and advanced-queries rules, then applies eager loading, withCount, and composite index guidance with verified syntax. ## Quick Start Ask the AI to review or write Laravel code, such as refactoring a controller with N+1 queries or creating a queued job with proper retry and backoff configuration.

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?

Eager load relationships with with() before iterating over models, and enable Model::preventLazyLoading() in AppServiceProvider during development to throw exceptions on lazy loading. Use withCount() when only relationship counts are needed instead of hydrating related models.

How should I structure Laravel controllers and routes?

Organize controllers around one resource using standard actions like index, show, store, update, and destroy, and use Route::resource() or apiResource() for matching endpoints. Treat custom verbs like publish as signals to consider a separate resource controller with its own authorization boundary.

What is the best way to handle Laravel queue job retries?

Set retry_after longer than the job timeout, use progressive backoff delays for transient failures, and implement ShouldBeUnique when only one instance of a logical job should exist. Make important jobs idempotent since workers can stop after side effects but before acknowledging.

Does Laravel validation replace authorization checks?

No, validation only establishes the shape and values of input and does not authorize the user. Use policies, gates, or a form request's authorize() method for permission checks, and never pass $request->all() to models with $guarded = [].

When should I use chunk() versus cursor() in Laravel?

Use chunk() or lazy() when processing large datasets with relationships, since cursor() cannot eager load. Use chunkById() or lazyById() when updates during iteration could shift offset-based pagination and cause rows to be skipped or processed twice.

Why should env() not be called outside config files in Laravel?

After configuration is cached, Laravel does not load the .env file, so env() calls in application code return null. Read environment variables only in configuration files and access them everywhere else through config().