<Rafi/>
LaravelSep 26, 2026 · 1 min read · by Ashikul Islam

Caching expensive queries in Laravel

20261017 011458 caching expensive queries in laravel 2cd724 cover

This is demo content for your developer blog. Replace it from Admin → Blog Posts.

The problem

Most performance issues come from doing too much work on every request. Measure first, then fix the slowest part.

💡 Tip: profile in production-like conditions — your laptop lies.

The solution

Here is the core idea in a few lines of PHP:

$stats = Cache::remember('dashboard.stats', now()->addMinutes(10), function () {
    // runs once every 10 minutes
    return Order::query()
        ->selectRaw('count(*) as orders, sum(total) as revenue')
        ->first();
});

⚠️ Warning: cache invalidation is the hard part — always set a TTL.

Results

  • Response time dropped from 820 ms to 140 ms.
  • Database load fell by 70%.
  • No new infrastructure needed.

✅ Best practice: add a regression test so the slowdown never comes back.

✎ Edit this page on GitHub