我如何才能在Laravel中使用此政策?

时间:2018-10-01 14:30:39

标签: php laravel-5 laravel-5.7

我有一个非常基本的政策

<?php

namespace App\Policies;

use App\Models\Comment;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class CommentPolicy
{
    use HandlesAuthorization;

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

我在视图中称呼它

@can('update', $comment)
Edit
@endcan

我注册了

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        App\Models\Comment::class => App\Policies\CommentPolicy::class,
    ];

尽管它应该总是显示,因为我已经对true进行了硬编码,但没有任何显示

1 个答案:

答案 0 :(得分:0)

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        \App\Models\Comment::class => \App\Policies\CommentPolicy::class,
    ];

我忘了开头的反斜杠