Cache a prop and forget it on write

Cache::remember stores a public count. Forget that key when a post is created. Do not cache one user's drafts under a global key.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/laravel-cache-remember-vue.md
Cache::remember stores a public count. Forget that key when a post is created. Do not cache one user's drafts under a global key.
Do not cache one user's drafts under a global key.
Do not Cache::remember a paginator that called where('userid', auth()->id()) under a key with no user id in it.
Do not cache the rendered Inertia page in a way that includes the CSRF token.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel cache Vue page” remember the whole post list, including drafts, under one key. The next visitor sees the first visitor’s rows. Cache a public number. The page that shows private rows stays on the CRUD guide query.

TL;DR

  • Cache::remember stores a public count.
  • Forget that key when a post is created.
  • Do not cache one user’s drafts under a global key.
  • Do not Cache::remember a paginator that called where('user_id', auth()->id()) under a key with no user id in it.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
$stats = Cache::remember('stats.posts', now()->addMinutes(5), function () {
    return ['count' => Post::query()->count()];
});

Pass $stats as a prop. When a post is created or deleted, forget the key in that same request:

php
Post::create([...]);
Cache::forget('stats.posts');

Five minutes is a choice for a homepage count, not for an editor.

What to avoid#

Gotcha

Do not Cache::remember a paginator that called where('user_id', auth()->id()) under a key with no user id in it. Do not cache the rendered Inertia page in a way that includes the CSRF token. That token belongs to one session.

FAQ#

How do I cache a prop and forget it on write?

Cache::remember stores a public count. Forget that key when a post is created. Do not cache one user's drafts under a global key.

What goes wrong with Laravel cache Vue page?

Many developers remember the whole post list, including drafts, under one key. Do not Cache::remember a paginator that called where('user_id', auth()->id()) under a key with no user id in it.

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.