laravel-best-practices

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

Updated Sep 5, 2025
One-click install
npx skills add https://github.com/baubyte/website --skill laravel-best-practices-baubyte
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: laravel-best-practices
Source: https://github.com/baubyte/website/tree/main/.agents/skills/laravel-best-practices
Command: npx skills add https://github.com/baubyte/website --skill laravel-best-practices-baubyte

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 rule set covering the full Laravel stack so code is written and reviewed against consistent, proven patterns. ## Core Features & Use Cases - Indexed Rule Library: Twenty rule files covering Eloquent, migrations, validation, routing, security, caching, queues, scheduling, testing, and architecture, each with incorrect/correct code examples. - Consistency-First Guidance: Instructs checking existing codebase patterns before applying any rule, so refactors match the project's established conventions. - Performance & Security Focus: Concrete fixes for N+1 queries, missing indexes, mass assignment, SQL injection, XSS, and queue misconfiguration. - Use Case: When reviewing a pull request that adds a new controller with Eloquent queries, map the changes to the routing, eloquent, and db-performance rule files and verify the code against each pattern before approving. ## Quick Start Review this Laravel controller and its Eloquent queries against the best-practice rules and fix any N+1 or validation issues.

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?▼

Use eager loading with the with() method to load relationships upfront instead of lazy loading inside loops. You can also enable Model::preventLazyLoading() in AppServiceProvider during development to throw exceptions when lazy loading occurs.

How to validate requests in Laravel controllers?▼

Extract validation into dedicated Form Request classes and type-hint them in controller methods, which triggers automatic validation and authorization. Always use $request->validated() for mass operations instead of $request->all().

What is the difference between cursor() and lazy() in Laravel?▼

cursor() keeps one model in memory but cannot eager-load relationships, risking N+1 issues. lazy() returns a chunked LazyCollection that supports eager loading, making it the correct choice when iterating records with relationships.

Does Laravel queue retry configuration cause duplicate job execution?▼

Yes, if retry_after in config/queue.php is shorter than the job's timeout, workers re-dispatch the job while it is still running. Always set retry_after greater than the longest job timeout and implement ShouldBeUnique for idempotency.

When should I use global scopes versus local scopes in Eloquent?▼

Reserve global scopes for universal constraints like soft deletes or multi-tenancy, since they silently modify every query. For conditional filters like published status, use local scopes that callers explicitly opt into.