---
title: 'Find N+1 queries in a Laravel snippet'
description: 'Run a small Eloquent snippet, inspect captured SQL, and compare the query shape before and after eager loading or aggregate counts.'
canonical_url: 'https://larabench.com/laravel-tinker-workflows/find-laravel-n-plus-one-queries'
md_url: 'https://larabench.com/laravel-tinker-workflows/find-laravel-n-plus-one-queries.md'
last_updated: '2026-07-28'
---
# Find N+1 queries in a Laravel snippet

> Run an Eloquent snippet in LaraBench, then compare its SQL before and after adding withCount() or eager loading.

## Direct Answer

Run a deliberately small Eloquent snippet, inspect the SQL it produces, then replace repeated relationship queries with eager loading or aggregate counts. LaraBench records the snippet, output, and captured SQL together in History.

## When To Use This

Use this when a page, job, export, or API endpoint feels slower than expected and you can reproduce its database access with a small snippet.

## Terminal Approach

```bash
php artisan tinker
App\Models\User::query()->latest()->limit(20)->get();
```

## LaraBench Approach

Open Run Code, choose the right connection, run the snippet in Tinker mode, open the SQL queries panel, look for repeated query shapes, then rerun a fixed snippet.

## Snippet

```php
use App\Models\User;

$users = User::query()->latest()->limit(20)->get();

return $users->map(fn (User $user) => [
    'id' => $user->id,
    'email' => $user->email,
    'orders' => $user->orders()->count(),
]);
```

Compare with:

```php
use App\Models\User;

$users = User::query()->withCount('orders')->latest()->limit(20)->get();

return $users->map(fn (User $user) => [
    'id' => $user->id,
    'email' => $user->email,
    'orders' => $user->orders_count,
]);
```

## Expected Result

The first run often shows one users query plus repeated count queries. The second run should produce the same output shape with a lower query count.

## Troubleshooting

- If the model class is not found, import the real app model.
- If the SQL panel is empty, confirm query capture is enabled and the snippet touches the database.
- If the fixed version changes the result, check scopes, soft deletes, and relationship constraints.

## Safety Notes

Keep the snippet read-only. Avoid relationship methods that create, update, delete, dispatch jobs, or fire business side effects while you are only trying to inspect SQL.

## Related Sources

- [Laravel eager loading documentation](https://laravel.com/docs/13.x/eloquent-relationships#eager-loading)
- [Laravel database query listener documentation](https://laravel.com/docs/13.x/database#listening-for-query-events)
- [LaraBench SQL queries docs](https://larabench.com/docs/basic-usage/sql-queries)

## Related Workflows

- [Benchmark Eloquent and query builder code](https://larabench.com/laravel-tinker-workflows/benchmark-eloquent-queries)
- [Inspect Eloquent models and collections](https://larabench.com/laravel-tinker-workflows/inspect-eloquent-models-and-collections)

## Canonical Page

- [HTML page](https://larabench.com/laravel-tinker-workflows/find-laravel-n-plus-one-queries)
- [Markdown mirror](https://larabench.com/laravel-tinker-workflows/find-laravel-n-plus-one-queries.md)

## Glossary

- [Laravel workflow glossary](https://larabench.com/docs/reference/glossary)

## Sitemap

See the full [sitemap](https://larabench.com/sitemap.md) for all public pages.
