iserter avatar

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-code

Installs 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

More from iserter/laravel-claude-agents

All 2
1cap-go avatarcapacitor-securitycap-go/capgo-skillsComprehensive security guide for Capacitor apps using Capsec scanner. Covers 63+ security rules across secrets, storage, network, authentication, cryptography, 1.3Kskills.sh installs
2next-safe-action avatarsafe-action-clientnext-safe-action/skillsUse when creating or configuring a next-safe-action client, defining actions with input/output validation, handling server errors, or setting up createSafeActio1.2Kskills.sh installs
3jezweb avatarshopify-contentjezweb/claude-skillsCreate and manage Shopify pages, blog posts, navigation menus, redirects, and SEO metadata via the Admin API or browser automation. Use whenever the user wants 1.2Kskills.sh installs
4next-safe-action avatarsafe-action-advancednext-safe-action/skillsUse when working with bind arguments, metadata schemas, framework errors (redirect/notFound/forbidden/unauthorized), type inference utilities (InferSafeActionFn1.2Kskills.sh installs
5vtex avatarvtex-io-app-settingsvtex/skillsApply when defining, validating, or consuming VTEX IO app settings. Covers settingsSchema, app-level configuration boundaries, and how backend or frontend code 1.2Kskills.sh installs
6davila7 avatarsenior-architectdavila7/claude-code-templatesComprehensive software architecture skill for designing scalable, maintainable systems using ReactJS, NextJS, NodeJS, Express, React Native, Swift, Kotlin, Flut1.2Kskills.sh installs