eloquent-best-practices — an installable skill for AI agents, published by iserter/laravel-claude-agents.
Laravel Eloquent query optimization and relationship management patterns with N+1 prevention.
Eager load relationships with with() and use withCount() for efficient aggregations instead of triggering additional queries in loops
Select only needed columns at query time and define return types on relationship methods for better IDE support and clarity
Use query scopes to encapsulate reusable filtering logic and leverage database-level operations (update(), increment()) instead of loading models into memory
Implement mass assignment protection via $fillable or $guarded, define type casts for safety, and chunk large datasets to manage memory efficiently
Add database indexes on foreign keys and frequently queried columns, and enable lazy loading prevention in development to catch N+1 issues early
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
}don't have the plugin yet? install it then click "run inline in claude" again.