Laravel Eloquent
**Priority: P0 (CRITICAL)**
Structure
app/
└── Models/
├── {Model}.php
└── Scopes/ # Advanced global scopes
Implementation Guidelines
- N+1 Prevention: Always use
with() or $with for relationships.
- Eager Loading: Set strict loading via
Eloquent::preventLazyLoading().
- Reusable Scopes: Define
scopeName methods for common query filters.
- Mass Assignment: Define
$fillable and use $request->validated().
- Performance: Use
chunk(), lazy(), or cursor() for large tasks.
- Casting: Use
$casts for dates, JSON, and custom types.
Anti-Patterns
- N+1 Queries: No lazy loading: Never query relationships in loops.
- Fat Models: No business logic: Models are for data access only.
- Magic Queries: No raw SQL: Use Query Builder or Eloquent.
- Select \*: No excessive data: Select only required columns.
References
Use this skill with your AI agent:
Open on Skill Issue to copy, download, or add to your library.
Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.