-1

I have a table called agents and I want to check in middleware when the agent is trying to log in, the agent is active or not

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

      if(agentmodel::where('status') == 'active'){
           true;
      }else{
           false;
      }
}
itxrahulsingh
  • 53
  • 1
  • 11

2 Answers2

2

You have to return the request in the middleware so it continues

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');
}

This will redirect back and not allow the user to continue if the agent is not active

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
-3
public function handle($request, Closure $next)
{
      $user = $this->auth->user();
      $agent = AgentModel::where("status", "active")->get();

      if(count($agent) > 0) {
           return $next($request);
      } else {
           return back();
      }
}
Sok Chanty
  • 1,678
  • 2
  • 12
  • 22