请问一个 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 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

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

3年前 评论

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

3年前 评论

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