xxxxxxxxxx
DB::beginTransaction();
try { /** Statement */ DB::commit(); }
catch (\Exception $e) { /** Statement if failed */ DB::rollback(); }
xxxxxxxxxx
DB::beginTransaction();
try {
$project = Project::find($id);
$project->users()->detach();
$project->delete();
DB::commit();
} catch (\Exception $ex) {
DB::rollback();
return response()->json(['error' => $ex->getMessage()], 500);
}
xxxxxxxxxx
DB::beginTransaction();
try {
DB::insert( );
DB::insert( );
DB::insert( );
DB::commit();
// all good
} catch (\Exception $e) {
DB::rollback();
// something went wrong
}
xxxxxxxxxx
use Exception;
DB::beginTransaction();
try {
//your code;
DB::commit();
//return back();
} catch (Exception $e) {
DB::rollBack();
dd($e->getMessage());
}
//-----------or----------
try {
return DB::transaction(function () {
//your code;
return back();
});
} catch (Exception $e) {
return $e->getMessage();
}
//note: I prefer the first one.
xxxxxxxxxx
DB::beginTransaction();
try{
DB::Commit();
} catch (Exception $e) {
DB::rollback();
}
xxxxxxxxxx
// Start transaction!
DB::beginTransaction();
try {
// Validate, then create if valid
$newAcct = Account::create( ['accountname' => Input::get('accountname')] );
} catch(ValidationException $e)
{
// Rollback and then redirect
// back to form with errors
DB::rollback();
return Redirect::to('/form')
->withErrors( $e->getErrors() )
->withInput();
} catch(\Exception $e)
{
DB::rollback();
throw $e;
}
try {
// Validate, then create if valid
$newUser = User::create([
'username' => Input::get('username'),
'account_id' => $newAcct->id
]);
} catch(ValidationException $e)
{
// Rollback and then redirect
// back to form with errors
DB::rollback();
return Redirect::to('/form')
->withErrors( $e->getErrors() )
->withInput();
} catch(\Exception $e)
{
DB::rollback();
throw $e;
}
// If we reach here, then
// data is valid and working.
// Commit the queries!
DB::commit();
xxxxxxxxxx
DB::transaction(function()
{
$newAcct = Account::create([
'accountname' => Input::get('accountname')
]);
$newUser = User::create([
'username' => Input::get('username'),
'account_id' => $newAcct->id,
]);
});
xxxxxxxxxx
DB::beginTransaction();
try {
DB::insert( );
DB::commit();
} catch (\Exception $e) {
DB::rollback();
throw $e;
} catch (\Throwable $e) {
DB::rollback();
throw $e;
}
xxxxxxxxxx
// Start transaction
beginTransaction();
// Run Queries
$acct = createAccount();
$user = createUser();
// If there's an error
// or queries don't do their job,
// rollback!
if( !$acct || !$user )
{
rollbackTransaction();
} else {
// Else commit the queries
commitTransaction();
}