Laravel 8: Class 'App\Policies\Gate' 未找到

Laravel 8: Class 'App\Policies\Gate' not found

我定义了一个名为 UserPolicy 的策略,它是这样的:

class UserPolicy
{
    use HandlesAuthorization;

    public function edit(User $user)
    {
        if(Gate::allows('edit', $user)){
            return view('admin.users.edit' , compact('user'));
        }
        abort(403);
    }
}

AuthServiceProvider.php,我这样称呼它:

protected $policies = [
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
        User::class => UserPolicy::class
    ];

然后在 Blade,我添加了这个:

@can('edit', $user)
   <a href="{{ route('users.edit' , ['user' => $user->id]) }}" class="btn btn-sm btn-primary">Edit</a>
@endcan

但是现在,我得到这个错误:

Class 'App\Policies\Gate' not found

我该如何解决这个问题?

看起来您还没有在 UserPolicy class

中导入 Gate 门面
use Illuminate\Support\Facades\Gate;