1 回答
TA贡献2051条经验 获得超10个赞
当我的会话到期并重新加载页面时,我收到以下错误(在以下代码块中发出信号的行):
hasRole()错误:调用成员函数null
class AdminMiddleware
{
public function handle($request, Closure $next) {
if ($request->user()->hasRole('admin')) // this line
return $next( $request );
else
abort( 401, 'Esta acción no está autorizada.' );
}
}
hasRole()是我User模型中的一个函数,用于检查用户是否有权访问某些管理路由:
class User extends Authenticatable
{
/**
* Role-user relation (one-to-many)
*/
public function roles() {
return $this->belongsToMany( Role::class )->withTimestamps();
}
/**
* Authorize roles
*/
public function authorizeRoles($roles) {
if ($this->hasAnyRole($roles))
return true;
abort( 401, 'Esta acción no está autorizada.' );
}
/**
* Check if user has specified role
*/
public function hasRole($role) {
if ($this->roles()->where('name', $role)->first())
return true;
return false;
}
}
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报