请问一个 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 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

isAuthorOf 方法和教程一样吗?外键是 user_id 吗?

3年前 评论

谢谢@Mr-houzi 的回复! 是一样的。 后来我才发现是哪里错了。在Providers/AuthServiceProvider.php 要加一句: 类似 \App\Models\Topic::class => \App\Policies\TopicPolicy::class 的

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!