diff --git a/src/Illuminate/Session/DatabaseSessionHandler.php b/src/Illuminate/Session/DatabaseSessionHandler.php index 2fa630f973a8..61c77ee85d93 100644 --- a/src/Illuminate/Session/DatabaseSessionHandler.php +++ b/src/Illuminate/Session/DatabaseSessionHandler.php @@ -6,6 +6,7 @@ use Illuminate\Support\Arr; use SessionHandlerInterface; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Database\QueryException; use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Container\Container; @@ -123,14 +124,42 @@ public function write($sessionId, $data) } if ($this->exists) { - $this->getQuery()->where('id', $sessionId)->update($payload); + $this->performUpdate($sessionId, $payload); } else { - $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId)); + $this->performInsert($sessionId, $payload); } return $this->exists = true; } + /** + * Perform an insert operation on the session ID. + * + * @param string $sessionId + * @param string $payload + * @return void + */ + protected function performInsert($sessionId, $payload) + { + try { + return $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId)); + } catch (QueryException $e) { + $this->performUpdate($sessionId, $payload); + } + } + + /** + * Perform an update operation on the session ID. + * + * @param string $sessionId + * @param string $payload + * @return int + */ + protected function performUpdate($sessionId, $payload) + { + return $this->getQuery()->where('id', $sessionId)->update($payload); + } + /** * Get the default payload for the session. *