
Eloquent Best Practices
This is a solid reference for avoiding the performance traps that sneak into Laravel apps as they grow. It covers the big ones: eager loading to prevent N+1 queries, using withCount instead of counting in loops, chunking large datasets, and doing updates at the database level instead of loading everything into memory. The examples are clear about what's slow and what's fast, which is exactly what you need when you're staring at a slow query log. The tip about Model::preventLazyLoading in development is worth the price of admission alone since it'll catch relationship issues before they hit production. Useful for both Laravel beginners who need to learn these patterns and experienced developers who want a quick checklist during code review.
- skills.sh installs
- 1.3K
- GitHub stars
- 48
- last updated
- Aug 21, 2026
Install
$ npx -y skills add iserter/laravel-claude-agents --skill eloquent-best-practices --agent claude-codeInstalls into .claude/skills of the current project.
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
Sponsors
Details
- Repository
- github.com/iserter/laravel-claude-agents
- Categories
- Backend & APIs, PHP & Laravel, Databases