Fix N+1 Queries in Laravel, Measured Before and After

    1

    Your coding agent finds the N+1 queries and slow Eloquent calls in your Laravel app, fixes them one small change at a time, and shows you the numbers before and after each fix.

    Free

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    Fix N+1 Queries in Laravel, Measured Before and After

    Example session with this skill installed

    Our /orders page takes 3 seconds to load. I think it's N+1. Can you find what's slow and fix it?

    • Read your context and instructions
    • Compiled the fix n+1 queries

    Summary (example run). GET /orders ran 214 queries. Two N+1 problems and a missing index. After the fixes: 5 queries.

    Before and after

    EndpointQueriesSQL msPage msMemory
    GET /orders before2141,2403,05046 MB
    GET /orders after53826014 MB

    What I changed

    1. customer was loaded once per row. Added with('customer:id,name') in OrderController@index.
    2. $order->items()->sum('price') ran in the view for each order. Replaced with withSum('items', 'price') and read items_sum_price.
    3. EXPLAIN showed a full scan on orders filtered by status and sorted by created_at. Added a composite index (status, created_at). EXPLAIN after: index used, rows read went from 48,000 to 50.
    $orders = Order::with('customer:id,name')
        ->withSum('items', 'price')
        ->where('status', $status)
        ->latest()
        ->paginate(25);
    

    Locked with a test

    DB::enableQueryLog();
    $this->get('/orders')->assertOk();
    expect(count(DB::getQueryLog()))->toBeLessThan(10);
    

    Next steps

    • orders.index is also rendered by the export controller. Same with() needed there.
    • Run the migration on staging and check write speed on orders.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Your Laravel app got slow. A list page runs hundreds of queries, a job runs out of memory, an endpoint times out. You know it's probably N+1, but finding every one by hand takes a day.

    This skill gives your agent a clear method, and one rule: no fix without a number before and a number after.

    What it does

    • Measures the slow page, job or command first: query count, SQL time, page time, memory.
    • Turns on preventLazyLoading to catch lazy loads, then greps for the ones it misses, like ->comments()->count() inside a loop or an accessor that runs a query.
    • Fixes with the smallest change first: eager loading, withCount and withSum, constrained eager loads, indexes checked with EXPLAIN, chunkById and lazyById for big tables, caching last.
    • Checks your Laravel version before using a recipe, so you don't get Laravel 11 code on a Laravel 10 app.
    • Measures again and adds a test that fails if the query count comes back.

    What you get A before and after table per endpoint, the code changes, and a test that holds the gain. On a Laravel 13 test app seeded with a 3-level N+1 (200 posts, 1,000 comments): 1,404 queries down to 8.

    What it doesn't do It won't add an index without EXPLAIN, won't hide an N+1 behind a cache or a global auto eager load switch, and won't make up numbers. With no database to run on, it does a static audit and marks every number as estimated.

    Requires Laravel 10+. Per-parent eager load limits and Cache::flexible require Laravel 11+. Cannot optimize non-SQL drivers. Measurement requires a seeded database or production copy.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Free forever

    No account required to browse

    Trust & safety

    Security scan

    Security scan passed 3 days ago

    • Free with an Agensi account

    Requires

    Laravel 10+ app
    PHP 8.1+

    Permissions

    Terminal / Shell
    Write Files
    Read Files

    File Scopes

    app/**/*.php
    database/migrations/**/*.php
    tests/**/*.php
    Listed3 days ago

    What's inside

    Frequently Asked Questions

    Yes. It checks your version first. Recipes that need Laravel 11 or later are marked, with what to do on 10 instead.

    Only if you want it. It can measure with a small DB::listen logger instead, and if it adds Debugbar it tells you to remove it after.

    New in Data & Databases