Gate a Vue index with viewAny

viewAny takes the model class, not a row. Call it on the index before you paginate.

For your AI agent

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

Read https://laravel-vuejs.space/laravel-policy-view-any-vue.md
viewAny takes the model class, not a row. Call it on the index before you paginate.
Do not filter the rows in Vue after sending every post to the browser.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel viewAny Vue” call authorize('view', $post) on the index and then the index has no post to pass. viewAny is the index check. view is one row. The policy guide is the class. The CRUD guide is the list.

TL;DR

  • viewAny takes the model class, not a row.
  • Call it on the index before you paginate.
  • Do not filter the rows in Vue after sending every post to the browser.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
public function viewAny(User $user): bool
{
    return $user->can('posts.view');
}

public function index(Request $request): Response
{
    $this->authorize('viewAny', Post::class);

    return Inertia::render('posts/Index', [
        'posts' => Post::query()->latest('id')->paginate(12),
    ]);
}

The second argument is the class. An instance would call view, not viewAny.

What to avoid#

Gotcha

Do not filter the rows in Vue after sending every post to the browser. If the user may only see their own, scope the query as well as the gate. The gate answers “may this person open the index?”. The query answers “which rows?”.

FAQ#

How do I gate a Vue index with viewAny?

viewAny takes the model class, not a row. Call it on the index before you paginate.

What goes wrong with Laravel viewAny Vue?

Many developers call authorize('view', $post) on the index and then the index has no post to pass. Do not filter the rows in Vue after sending every post to the browser.

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.