← ClaudeAtlas

eloquent-patternslisted

Laravel guidance to keep Eloquent models explicit, query shapes intentional, relationship loading visible, and production data access bounded.
soden46/syarif-laravel-ai-skills · ★ 4 · AI & Automation · score 78
Install: claude install-skill soden46/syarif-laravel-ai-skills
# Eloquent Patterns Keep models explicit, query shape intentional, and relationship loading visible near the code that renders or returns data. ## Model Contracts Models should declare mass-assignment boundaries, casts, and concrete relationship return types. ```php class Record extends Model { protected $fillable = [ 'owner_id', 'number', 'total', 'issued_at', 'is_active', ]; protected function casts(): array { return [ 'issued_at' => 'date', 'total' => 'decimal:2', 'is_active' => 'boolean', 'metadata' => 'array', ]; } public function owner(): BelongsTo { return $this->belongsTo(User::class); } } ``` Use accessors sparingly for simple derived values. Move heavy behavior into Actions or Services. ## Relationship Loading Prevent N+1 queries by eager loading the relations a surface needs. Before rendering Blade reports, printable views, exports, or PDFs, explicitly load the relation graph. ```php public function show(Record $record): View { $record->load([ 'owner', 'items.product', 'approvals.user', ]); return view('records.show', ['record' => $record]); } ``` Do not add new relation dependencies inside templates without updating the load list and render tests. ## Query Volume Do not process unbounded production datasets with `all()` or broad `get()` calls. Use: - `paginate()` for nor