BadMethodCallException 调用未定义的方法 App\Models\User::hasAnyRole()

BadMethodCallException Call to undefined method App\Models\User::hasAnyRole()

我正在使用 Laravel-权限并在 rotes\web 中写道。php:

Route::group(['middleware' => ['role:admin']], function () {
    Route::get('/admin', function () {
        return "Test";
    });
});

我在app\Http\Kernel.php中添加了:

protected $routeMiddleware = [
    // ...
    'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
    'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
    'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];

错误:

确保您的 User 模型使用 Spatie\Permission\Traits\HasRoles 特征。

<?php

use Spatie\Permission\Traits\HasRoles;

class User
{
    use HasRoles;
}

您忘记包含角色特征

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles; //this line

    // ...
}