Home > Back-end >  Laravel 9 Gate "Too few arguments to function App\Providers\AuthServiceProvider::App\Provide
Laravel 9 Gate "Too few arguments to function App\Providers\AuthServiceProvider::App\Provide

Time:06-16

I have an Evaluation model that has a relationship to a User. I am trying to use Laravel Gates to protect the evaluation routes by allowing only the assigned users to access their evaluation(s). I have never used Gates before and cannot seem to get one to work and keep getting a Too few arguments to function App\Providers\AuthServiceProvider::App\Providers\{closure}(), 1 passed in /Users/charles/Documents/Development/seedgen/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 536 and exactly 2 expected error. I understand I need to pass in additional arguments, but I am stuck.

AuthServiceProvidor.php

...
public function boot()
{
    $this->registerPolicies();

    Gate::define('showEval', function (User $user, Evaluation $evaluation) {
        return $user->id === $evaluation->user_id;
    });
}
...

Show.php

...
public function render()
{

    if (Gate::allows('showEval')) {
        return view('livewire.evaluation.show');
    } else {
        abort(403);
    }

}
...

CodePudding user response:

you didn't pass the $evaluation to check in gate

public function render()
{
    // somehow getting $evaluation you want to pass to gate check
    if (Gate::allows('showEval', $evaluation)) {
        return view('livewire.evaluation.show');
    } else {
        abort(403);
    }

}

according to docs gate takes current user by itself, so passing second param is on you

  • Related