Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

Too few arguments passed down to policy

Hi, I just created a WishlistPolicy and in it I have a view rule, defined like this

 public function view(User $user, Wishlist $wishlist): bool
    {
        $wishlist->load('users');
        
        // Check if the user is the owner of the wishlist
        if ($wishlist->users && $wishlist->users->contains($user)) {
            return true;
        }

        return false;
    }

And where I use it I get a 500 error saying that Too few arguments to function App\Policies\WishlistPolicy::view(), 1 passed and exactly 2 expected

Gate::authorize('view', Wishlist::class);

Any idea why this is happening? And if I pass [User::class, Wishlist::class] down to the rule I get 403 error. I do not understand what is happening because I do not get this error in any other rule in this policy. Thanks in advance

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error you're encountering is due to the way you're calling the Gate::authorize method. The view method in your WishlistPolicy expects two arguments: a User instance and a Wishlist instance. However, when you call Gate::authorize('view', Wishlist::class);, you're only passing one argument, which is the class name of Wishlist, not an instance of it.

To fix this, you need to pass an instance of the Wishlist model to the Gate::authorize method. Here's how you can do it:

// Assuming you have a $wishlist instance of Wishlist
$wishlist = Wishlist::find($wishlistId); // Replace $wishlistId with the actual ID

// Now authorize using the instance
Gate::authorize('view', $wishlist);

Make sure that $wishlist is a valid instance of the Wishlist model. If you're getting a 403 error after passing the correct arguments, it means the authorization check is failing. This could be due to the logic in your view method. Double-check that the users relationship is correctly defined and that the contains method is being used correctly to check if the user is associated with the wishlist.

If the users relationship is a many-to-many relationship, ensure it's set up properly in your Wishlist model:

public function users()
{
    return $this->belongsToMany(User::class);
}

Also, ensure that the $user variable being passed is the currently authenticated user, which you can typically get using auth()->user().

If you continue to face issues, you might want to debug by checking the values of $wishlist->users and $user to ensure they are what you expect.

Please or to participate in this conversation.