Agent skill
laravel-eloquent
Develops Eloquent models, relationships, scopes, and query optimization. Activates when creating models, defining relationships (hasMany, belongsTo, morphTo), writing scopes, eager loading, query building, accessors/mutators, model events, or when user mentions model, relationship, query, N+1, eager load, scope, or database optimization.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/laravel-eloquent
SKILL.md
Laravel Eloquent Development
When to Apply
Activate this skill when:
- Creating or modifying Eloquent models
- Defining model relationships
- Writing query scopes (local or global)
- Optimizing queries or fixing N+1 problems
- Working with accessors, mutators, or casts
- Implementing model events or observers
Documentation
Use search-docs for detailed Eloquent patterns and documentation.
Basic Usage
Creating Models
Use Artisan to create models with related files:
php artisan make:model Post --migration --factory --seeder
Relationships
Always use explicit return types on relationship methods:
public function posts(): HasMany { return $this->hasMany(Post::class); }
public function author(): BelongsTo { return $this->belongsTo(User::class, 'author_id'); }
Eager Loading
Prevent N+1 queries by eager loading relationships:
// Bad - causes N+1 queries $posts = Post::all(); foreach ($posts as $post) { echo $post->author->name; // Query per post }
Constraining Eager Loads
Local Scopes
public function scopePublished(Builder $query): Builder { return $query->where('published_at', '<=', now()); }
public function scopeByAuthor(Builder $query, User $author): Builder { return $query->where('author_id', $author->id); }
// Usage Post::published()->byAuthor($user)->get();
Casts
Define casts in the casts() method (Laravel 11+):
Accessors and Mutators
Use attribute casting for simple transformations:
protected function fullName(): Attribute { return Attribute::make( get: fn () => "{$this->first_name} {$this->last_name}", ); }
protected function email(): Attribute { return Attribute::make( set: fn (string $value) => strtolower($value), ); }
Query Building
Prefer Model::query() over DB:: facade:
// Avoid $posts = DB::table('posts')->where(...)->get();
Model Events and Observers
public function deleted(Post $post): void
{
Storage::delete($post->featured_image);
}
}
Common Pitfalls
- Not using eager loading, causing N+1 queries
- Using
DB::instead of Eloquent models - Missing return types on relationship methods
- Forgetting to create factories and seeders with new models
- Using
$castsproperty instead ofcasts()method in Laravel 11+ - Not constraining eager loads when only subset of data is needed
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?