使用@can Blade 指令时出现参数计数错误

Parameter count error when using @can Blade directive

当我尝试使用策略检查权限时遇到以下错误:

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 这只是我的代码中导致错误的部分

@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

我什至尝试用 App\Answer::class 替换 $answer,但我仍然遇到完全相同的错误。

您可以在以下位置查看完整的错误堆栈: https://flareapp.io/share/xPQQQXP1#F66

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

我在您的 @can 语句中没有看到 Answer 实例。一个存在吗?如果是这样,您应该这样做:

@can('view', $answer)

或者,如果此时不存在,请像这样定义您的方法

public function view(User $user)

并这样称呼它:

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