首页 > 解决方案 > 检查中间件中的状态是否处于活动状态

问题描述

我有一个名为的表agents,我想在middleware代理尝试登录时签入,代理是否处于活动状态

public function handle($request, Closure $next)
   {
      $user = $this->auth->user();

      if(agentmodel::where('status') == 'active'){
           true;
      }else{
           false;
      }
}

标签: phpmysqllaravellaravel-middleware

解决方案


您必须在中间件中返回请求才能继续

public function handle($request, Closure $next)
{
    // Declared but not used
    $id = auth()->id();
    // If agent belongs to a user
    if (App\agentmodel::where('user_id', $id)->first()->status == 'active') {
        return $next($request);
    }
    \Session::flush();
    return back()->with('error', 'Your account is not active');
}

如果代理不活动,这将重定向回来并且不允许用户继续


推荐阅读