title: How to update a field in the database upon user session expiration in Laravel
body: <p>I'm allowing the session to expire in session.php after 120 minutes of inactivity. Is there a way to trigger an update in the DB upon session expiration?</p>
  
  <p>I'm trying the below code, and only the first part works where the user is logged in  :</p>
  
  <pre><code>    public function handle($request, Closure $next)
      {
  
          if (\Auth::check()) {
          // The user is logged in...
          $user = \Auth::user();
          $user-&gt;lastActive = date('Y-m-d H:i:s');
          $user-&gt;online = 1;
          $user-&gt;save();
          }
          elseif (\Auth::logout()) {
  
          $user-&gt;lastActive = date('Y-m-d H:i:s');
          $user-&gt;online = 0;
          $user-&gt;save();
  
          }
  
  </code></pre>