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

44u64gxh  于 2023-01-04  发布在  PHP
关注(0)|答案(3)|浏览(130)

我正在使用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,
];

错误:

7gcisfzg

7gcisfzg1#

你忘了加入角色特性

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

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

    // ...
}
wsxa1bj1

wsxa1bj12#

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

<?php

use Spatie\Permission\Traits\HasRoles;

class User
{
    use HasRoles;
}
5lwkijsr

5lwkijsr3#

我想你没有加上这一点:
第一个月

class User
{
    use HasRoles;
}

相关问题