请问一个 authorize的问题
各位学长好,后学在此请教一问题,代码如下:
/**
* show
* 显示作业
*/
public function show(StudentWork $studentWork){
$this->authorize('show',$studentWork);
return new StudentWorkResource($studentWork);
}
调用后,报403:
这个 studentWork = 1 是这个用户的.
如果不写 $this->authorize(‘show’,$studentWork);
是能正常返回的。
在StudentWorkPolicy.php文件中,代码是:
class StudentWorkPolicy extends Policy
{
public function show(User $user, StudentWork $studentWork)
{ return $user->isAuthorOf($studentWork);
}
}
我对比了课程中的内容,不知问题在哪里,请问哪位学长能指点吗?
非常感谢!
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
isAuthorOf 方法和教程一样吗?外键是 user_id 吗?
谢谢@Mr-houzi 的回复! 是一样的。 后来我才发现是哪里错了。在Providers/AuthServiceProvider.php 要加一句: 类似 \App\Models\Topic::class => \App\Policies\TopicPolicy::class 的