Illuminate\Database\Connection::rollBack PHP Method

rollBack() public method

Rollback the active database transaction.
public rollBack ( integer | null $toLevel = null ) : void
$toLevel integer | null
return void
    public function rollBack($toLevel = null)
    {
        if (is_null($toLevel)) {
            $toLevel = $this->transactions - 1;
        }
        if ($toLevel < 0 || $toLevel >= $this->transactions) {
            return;
        }
        if ($toLevel == 0) {
            $this->getPdo()->rollBack();
        } elseif ($this->queryGrammar->supportsSavepoints()) {
            $this->getPdo()->exec($this->queryGrammar->compileSavepointRollBack('trans' . ($toLevel + 1)));
        }
        $this->transactions = $toLevel;
        $this->fireConnectionEvent('rollingBack');
    }

Usage Example

 /**
  * @param mixed $id
  * @param DomainEventStream $eventStream
  */
 public function append($id, DomainEventStream $eventStream)
 {
     $id = (string) $id;
     //Used to thrown errors if ID will not cast to string
     $this->db->beginTransaction();
     try {
         foreach ($eventStream as $domainMessage) {
             $this->insertEvent($domainMessage);
         }
         $this->db->commit();
     } catch (QueryException $ex) {
         $this->db->rollBack();
         throw $ex;
     }
 }