phplaravellaravel-bladelaravel-authorization

Parameter count error when using @can Blade directive


I'm having the following error when I try to check a permission using policies:

Too few arguments to function App\Policies\AnswerPolicy::view(), 1 passed in /Users/georgio/Projects/Laravel/municipality-app/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 710 and exactly 2 expected (View: /Users/georgio/Projects/Laravel/municipality-app/resources/views/layouts/loggedin.blade.php)

AnswerPolicy

<?php

namespace App\Policies;

use App\Answer;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class AnswerPolicy
{
    use HandlesAuthorization;


    public function view(User $user, Answer $answer)
    {
      return true;
    }

    public function edit(User $user)
    {
      return true;
    }

    public function create(User $user)
    {
      return true;
    }

    public function delete(User $user)
    {
      return true;
    }

    public function update(User $user)
    {
      return true;
    }
}

AnswersController

<?php

namespace App\Http\Controllers;

use App\Question;
use App\Answer;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AnswersController extends Controller
{
  public function index()
  {
    $this->authorize('view');
    return view('answers.show')->with('questions', Question::all());
  }

  public function create($questionId)
  {
    $this->authorize('view-submit-answer');
    return view('answers.create')->with('question',Question::find($questionId));
  }

  public function store()
  {
    $this->authorize('');
    $this->validate(request(),[

      'answer' => 'required',


    ]);

    $data = request()->all();

    $answer = new Answer();
    $answer->answer = $data['answer'];
    $answer->question_id = $data['question_id'];
    $answer->user_id = Auth::id();




    $answer->save();

    return redirect('/questions/answers');

  }

  public function edit($answerId)
  { $this->authorize('view-edit-answer');
    return view('answers.edit')->with('answer', Answer::find($answerId));
  }


  public function update($answerId)
  {
    $this->validate(request(),[

      'answer' => 'required',


    ]);

    $data = request()->all();

    $answer = Answer::find($answerId);
    $answer->answer = $data['answer'];


    $answer->save();

    return redirect('/answers-question');
  }

  public function destroy($questionID)
  {
    $this->authorize('delete-answer');
    $answer = Answer::where('question_id', $questionID)->where('user_id', Auth()->id());
    $answer->delete();

    return redirect('/questions/answers');
  }



}

loggedin.blade.php This is only the part of my code that is causing the error

@can('view', App\Answer::class)
 <li>
   <a href="/questions/answers">
     <span><i class="fa fa-pencil"></i></span>
     <span>Answer Question</span>
   </a>
 </li>
@endcan

I even tried replacing $answer with App\Answer::class and I still had the exact same error.

You can check the full error stack at: https://flareapp.io/share/xPQQQXP1#F66


Solution

  • The documentation says

    When defining policy methods that will not receive a model instance, such as a create method, it will not receive a model instance. Instead, you should define the method as only expecting the authenticated user

    I don't see an Answer instance inside your @can statement there. Does one exist? If so, you should be doing this:

    @can('view', $answer)
    

    Or, if one doesn't exist at that point, define your method like this

    public function view(User $user)
    

    And call it like this:

    @can('view', \App\Answer::class)