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-prevent-lazy-loading.md

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

Enable preventLazyLoading in Development

Impact: CRITICAL (Catches N+1 bugs before they reach production)

Without lazy loading prevention, N+1 query bugs are invisible — code works correctly but fires hundreds of unnecessary queries. Enabling preventLazyLoading in non-production environments throws an exception the moment a relationship is lazily loaded, forcing developers to fix the problem immediately.

Incorrect

// ❌ No lazy loading prevention — N+1 bugs silently reach production
class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Nothing here to catch lazy loading
    }
}

// This code works but fires 101 queries — nobody notices until production crawls
$posts = Post::all();
foreach ($posts as $post) {
    echo $post->author->name; // Silent N+1, no error raised
}

Problems:

  • N+1 queries go completely undetected during development and code review
  • Performance regressions accumulate silently until production degrades noticeably
  • Developers have no feedback loop to learn about missing eager loads

Correct

// ✅ Prevent lazy loading in non-production environments
class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Model::preventLazyLoading(! $this->app->isProduction());
    }
}

// Now this code throws LazyLoadingViolationException in dev/staging:
$posts = Post::all();
foreach ($posts as $post) {
    echo $post->author->name; // 💥 LazyLoadingViolationException thrown
}

// Developer is forced to fix it:
$posts = Post::with('author')->get();
foreach ($posts as $post) {
    echo $post->author->name; // ✅ No exception, 2 queries total
}

Benefits:

  • Throws LazyLoadingViolationException immediately when a relationship is lazily loaded
  • Catches N+1 problems during development before they reach production
  • Safe to enable — only active in non-production environments, so production is never affected

Reference: Laravel Preventing Lazy 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