
Eloquent Best Practices
Laravel Eloquent guidance for avoiding N+1 queries, tuning selects, and structuring models safely.
- skills.sh installs
- 1.3K
- GitHub stars
- 48
- last updated
- Aug 21, 2026
Own this skill? Claim it
What it does
- Covers query optimization, relationship typing, mass assignment, casts, chunking, and model events for Laravel apps
- Includes before-and-after PHP examples for eager loading,
withCount, query scopes, and bulkupdatecalls - Enforces
$fillablewhitelisting over empty$guarded, andModel::preventLazyLoadingoutside production - Designed for Laravel developers reviewing Eloquent code for performance problems and common pitfalls
Install
$ npx -y skills add iserter/laravel-claude-agents --skill eloquent-best-practices -yInstalls into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.
Files
Eloquent Best Practices
Query Optimization
Always Eager Load Relationships
// ❌ N+1 Query Problem
$posts = Post::all();
foreach ($posts as $post) {
echo $post->user->name; // N additional queries
}
// ✅ Eager Loading
$posts = Post::with('user')->get();
foreach ($posts as $post) {
echo $post->user->name; // No additional queries
}
Select Only Needed Columns
// ❌ Fetches all columns
$users = User::all();
// ✅ Only needed columns
$users = User::select(['id', 'name', 'email'])->get();
// ✅ With relationships
$posts = Post::with(['user:id,name'])->select(['id', 'title', 'user_id'])->get();
Use Query Scopes
// ✅ Define reusable query logic
class Post extends Model
{
public function scopePublished($query)
{
return $query->where('status', 'published')
->whereNotNull('published_at');
}
public function scopePopular($query, $threshold = 100)
{
return $query->where('views', '>', $threshold);
}
}
// Usage
$posts = Post::published()->popular()->get();
Relationship Best Practices
Define Return Types
Details
- Repository
- github.com/iserter/laravel-claude-agents
- Categories
- PHP & Laravel, Databases
Similar to Eloquent Best Practices
More PHP & Laravel
laravel-best-practicesasyrafhussin/agent-skills2.4K installsLaravel 13 conventions covering architecture, Eloquent, validation, and security with 31 rules across 7 categories.
laravel-database-optimizationasyrafhussin/agent-skills1.1K installsPerformance rule set for Laravel 13 covering N+1 queries, indexing, Redis caching, pagination, and transactions.
laravelmindrally/skills870 installsExpert guidance for Laravel PHP work covering MVC structure, Eloquent, validation, testing, and performance.
laravel-developmentmindrally/skills959 installsExpert Laravel guidance that applies SOLID principles, PSR-12 style, and Laravel conventions to PHP code.
laravel-patternsaffaan-m/ecc6.7K installsLaravel architecture patterns for controllers, Eloquent, services, queues, and API resources in production apps.
laravel-specialistjeffallan/claude-skills24.1K installsLaravel 10+ specialist covering Eloquent, Sanctum, Horizon, API resources, Livewire, and Pest/PHPUnit testing.