diff --git a/src/Illuminate/Session/DatabaseSessionHandler.php b/src/Illuminate/Session/DatabaseSessionHandler.php index dbc83bb777cf..87c1e7d3d00e 100644 --- a/src/Illuminate/Session/DatabaseSessionHandler.php +++ b/src/Illuminate/Session/DatabaseSessionHandler.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use SessionHandlerInterface; +use Illuminate\Database\QueryException; use Illuminate\Database\ConnectionInterface; class DatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface @@ -95,16 +96,45 @@ public function read($sessionId) public function write($sessionId, $data) { if ($this->exists) { - $this->getQuery()->where('id', $sessionId)->update([ - 'payload' => base64_encode($data), 'last_activity' => time(), - ]); + $this->performUpdate($sessionId, $data); } else { - $this->getQuery()->insert([ + $this->performInsert($sessionId, $data); + } + + $this->exists = true; + } + + + /** + * Perform an insert operation on the session ID. + * + * @param string $sessionId + * @param string $data + * @return void + */ + protected function performInsert($sessionId, $data) + { + try { + return $this->getQuery()->insert([ 'id' => $sessionId, 'payload' => base64_encode($data), 'last_activity' => time(), ]); + } catch (QueryException $e) { + $this->performUpdate($sessionId, $data); } + } - $this->exists = true; + /** + * Perform an update operation on the session ID. + * + * @param string $sessionId + * @param string $data + * @return int + */ + protected function performUpdate($sessionId, $data) + { + return $this->getQuery()->where('id', $sessionId)->update([ + 'payload' => base64_encode($data), 'last_activity' => time(), + ]); } /**