All skills
asyrafhussin avatar

/laravel-database-optimization

@ac26821

Laravel database optimization patterns. Use when writing Eloquent queries, creating migrations, configuring caching, debugging slow queries, or optimizing database performance. Triggers on tasks involving N+1 queries, indexing, Redis caching, pagination, or database transactions.

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/laravel-database-optimization

This session only. Nothing lands on disk.

rulesquery-auto-eager-loading.md

≈605 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Use Automatic Eager Loading (Laravel 13+)

Impact: CRITICAL (Eliminates N+1 queries without manual with() calls)

Laravel 13 introduces automatic eager loading, which detects when multiple models in a collection access the same relationship and batches those queries automatically. This eliminates the most common source of N+1 bugs without requiring developers to remember every with() call.

Incorrect

// ❌ Manually adding with() everywhere — easy to miss some
$posts = Post::with(['author', 'tags', 'comments'])->get();

// Elsewhere in the codebase, someone forgets:
$posts = Post::where('published', true)->get();
foreach ($posts as $post) {
    echo $post->author->name; // N+1 — forgot with('author')
}

// Or in a Blade template where with() can't be added:
@foreach ($posts as $post)
    {{ $post->category->name }} {{-- N+1 — no way to fix from the view --}}
@endforeach

Problems:

  • Every query site must remember to include the right with() calls
  • Blade templates and third-party packages cannot add with() themselves
  • Missing a single eager load silently introduces N+1 problems

Correct

// ✅ Option 1: Enable globally in AppServiceProvider
class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Model::automaticallyEagerLoadRelationships();
    }
}

// Now any collection access auto-batches relationship queries:
$posts = Post::where('published', true)->get();
foreach ($posts as $post) {
    echo $post->author->name; // Auto-batched — no N+1
}

// ✅ Option 2: Enable per-collection for targeted use
$posts = Post::where('published', true)->get();
$posts->withRelationshipAutoloading();

foreach ($posts as $post) {
    echo $post->author->name;       // Auto-batched
    echo $post->category->name;     // Auto-batched
}

Benefits:

  • Eliminates N+1 queries globally without modifying existing query code
  • Works in Blade templates, packages, and any code that iterates collections
  • Per-collection option provides fine-grained control when global activation is too broad

Reference: Laravel Automatic Eager Loading

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill is a comprehensive documentation set for optimizing database performance in Laravel 13 applications. It provides best practices for Eloquent queries, indexing, Redis caching, and safe migrations. No malicious patterns, prompt injections, or data exfiltration attempts were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    6/39 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at ac26821. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "1.1.1"
}

README badge

README badge for asyrafhussin/agent-skills/laravel-database-optimization