Added transaction method to database connection and eloquent model.
This commit is contained in:
@@ -84,6 +84,33 @@ class Connection {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback wrapped in a database transaction.
|
||||
*
|
||||
* @param Closure $callback
|
||||
* @return void
|
||||
*/
|
||||
public function transaction($callback)
|
||||
{
|
||||
$this->pdo->beginTransaction();
|
||||
|
||||
// After beginning the database transaction, we will call the Closure
|
||||
// so that it can do its database work. If an exception occurs we'll
|
||||
// rollback the transaction and re-throw back to the developer.
|
||||
try
|
||||
{
|
||||
call_user_func($callback);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->pdo->rollBack();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->pdo->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL query against the connection and return a single column result.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user