Skip to main content

Laravel Prompt to Optimize Eloquent Queries

Analyze and optimize Laravel Eloquent queries — kill N+1 problems, add indexes, add caching, and cut database load with before/after proof.

Füllen Sie die Platzhalter aus

Edit the values, then copy your finished prompt.

Ihr Prompt
prompt.txt

                                

What this prompt does

This prompt asks the AI to analyze and optimize specific Laravel Eloquent queries with before/after proof, not give generic advice. You paste [current_query] for your [model_name] model along with the table's [row_count] and its [related_tables], and the AI works through six fixes: N+1 problems via eager loading, missing indexes, over-selected columns, caching opportunities with [cache_driver], subquery-vs-join choices, and a pagination strategy — showing a query-count comparison via Laravel Debugbar and suggesting index migrations.

The structure works because it grounds the optimization in your real query and table shape. The [current_query] and [related_tables] variables tell the AI exactly where eager loading and joins matter, [row_count] signals when indexing and pagination become critical, and [cache_driver] (defaulting to Redis) targets the caching advice. Forcing a before/after query-count comparison makes the improvement provable rather than assumed.

When to use it

  • You have a slow Laravel page and suspect N+1 queries or missing indexes.
  • You want a specific query optimized with measurable before/after query counts.
  • You're querying a large table ([row_count] in the hundreds of thousands) and need a pagination strategy.
  • You want to know which columns and joins to cache with [cache_driver] and which not to.
  • You need an index migration suggested for the joins on [related_tables].

Example output

Expect a targeted optimization report: the rewritten [model_name] query with eager loading, a list of recommended indexes with a ready-to-run migration, columns to trim from the select, a caching approach using [cache_driver], a subquery-vs-join recommendation, and a pagination strategy suited to [row_count]. A before/after query-count comparison (framed around Laravel Debugbar) shows the N+1 collapse — analysis and code you can apply, not a vague checklist.

Pro tips

  • Paste the real [current_query] and any surrounding loop — the AI can't catch an N+1 it can't see.
  • List [related_tables] accurately so eager-loading and index suggestions cover the actual joins, not guesses.
  • Give a realistic [row_count]; it's what decides whether the AI recommends offset or cursor pagination.
  • Verify every suggested index against your real schema before migrating — a redundant index slows writes for no read gain.
  • Treat the before/after counts as a hypothesis to confirm with Debugbar on your machine, since the AI estimates without running your app.
  • Apply fixes one at a time (eager loading, then indexes, then caching) so you can attribute each query-count drop to a change.

Frequently Asked Questions

What information do I need to provide for good results?
Paste the actual `[current_query]` code, the `[model_name]`, the approximate `[row_count]`, and the `[related_tables]` it joins. The more accurate these are, the better the AI can spot N+1 problems and recommend the right indexes and eager loading.
Will it actually measure the performance improvement?
It frames a before/after query-count comparison using Laravel Debugbar, but the AI estimates rather than runs your app. Confirm the numbers with Debugbar locally, since the real improvement depends on your data and schema, not the AI's projection.
Which caching driver does it assume?
It targets the driver set in `[cache_driver]`, defaulting to Redis. The caching suggestions adapt to your driver, but you should evaluate which query results are safe to cache, since cached stale data can be worse than a slightly slower fresh query.
Should I apply all the suggested indexes?
Not blindly. Verify each suggested index against your real schema first, because redundant or overlapping indexes slow down writes without improving reads. Add indexes that support your actual query's filters and joins, then confirm the gain with query profiling.
Does it handle pagination for large tables?
Yes. Based on the `[row_count]` you provide, it recommends a pagination strategy. For very large tables, expect cursor or keyset pagination guidance rather than offset, since deep offset pagination degrades badly as the row count grows.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Mehr in Laravel & PHP Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support