Agent-skills-standard laravel-eloquent
Write performant Eloquent queries with eager loading, reusable scopes, and strict lazy-loading prevention in Laravel. Use when defining model relationships, creating query scopes, or processing large datasets with chunk/cursor. (triggers: app/Models/**/*.php, scope, with, eager, chunk, model)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/laravel/laravel-eloquent" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-laravel-eloquent && rm -rf "$T"
manifest:
skills/laravel/laravel-eloquent/SKILL.mdsource content
Laravel Eloquent
Priority: P0 (CRITICAL)
Workflow: Add Model with Safe Queries
- Define model — Set
,$fillable
, and relationships.$casts - Enable strict loading — Call
in AppServiceProvider.preventLazyLoading(!app()->isProduction()) - Add scopes — Create
,scopeActive()
for reusable filters.scopeVerified() - Eager-load in queries — Use
for all relationship access.with() - Process large datasets — Use
orchunk()
instead ofcursor()
.get()
Scope + Eager Loading Example
See implementation examples for model scopes, eager loading, and directory structure.
Implementation Guidelines
Query Efficiency & Performance
- N+1 Prevention: Always use
orwith()
for relationships. Never access relationship properties in loop without eager loading (N+1 Prevention).$with - Strict Loading: Call
inEloquent::preventLazyLoading(!app()->isProduction())
to throwAppServiceProvider::boot()
in local/dev.LazyLoadingViolationException - Large Datasets: Use
,chunk()
, orlazy()
for processing many records without memory issues (Memory Efficiency).cursor()
Query Logic & Scopes
- Reusable Scopes: Define
methods in models for reusable query filters.scopeName(Builder $query): Builder - Chaining: Chain scopes (e.g.,
) to keep controllers from duplicating query logic.User::active()->verified()->get()
Models & Security
- Mass Assignment: Always define
array; useprotected $fillable
for$request->validated()
.Model::create() - Casting: Use
for dates, JSON, and custom types to ensure data consistency.protected $casts
Anti-Patterns
- No lazy loading: Eager-load with
orwith()
; never in loops.$with - No business logic in Models: Move to Services or Actions.
- No raw SQL strings: Use Query Builder or Eloquent methods.
- No
: Specify required columns to limit data transfer.select *