我想限制管理员访问某些页面并在运行我的代码后收到此错误:传递给 App\Models\User::hasAnyRoles() 的参数 1 必须是数组类型,给定字符串,在 F:\Main Server 中调用\htdocs\voskillproject\app\Providers\AuthServiceProvider.php 在第 33 行(查看:F:\Main Server\htdocs\voskillproject\resources\views\backend\adminsidebar.blade.php)
这是我的用户模型
public function roles(){
return $this->belongsToMany('App\Models\Role');
}
/**
* check if the user has a role
* @param string $role
* @return bool
*/
public function hasAnyRole(string $role)
{
return null !== $this->roles()->where('Role_name',$role)->first();
}
/**
* check if the user has any given role
* @param array $role
* @return bool
*/
public function hasAnyRoles(array $role)
{
return null !== $this->roles()->whereIn('Role_name',$role)->first();
}
这是我的 authserviceprovider,我在其中注册了我的大门
Gate::define('is-admin',function($user)
{
return $user->hasAnyRoles('Super Admin','Company Manager');
});
这是导航栏部分
@can('is-admin')
<li class="nav-item {{ 'admin/roles'==request()->path()?'active':' ' }}">
<a class="nav-link" href="{{ route('roles.index') }}">
<i class="mdi mdi-view-headline menu-icon"></i>
<span class="menu-title">Roles</span>
</a>
</li>
@endcan
我不明白我的代码哪里做错了