laravel database transactions with eloquent code example
Example 1: laravel transactions
DB::beginTransaction();
try {
DB::insert(...);
DB::insert(...);
DB::insert(...);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
}
Example 2: DB::transaction
DB::beginTransaction();
try {
DB::insert(...);
DB::insert(...);
DB::insert(...);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
}
Example 3: TRANSACTON LARAVEL QUERY BUILDER
DB::beginTransaction();
try {
DB::insert(...);
DB::commit();
} catch (\Throwable $e) {
DB::rollback();
throw $e;
}
Example 4: laravel transaction query not working when multiple db connection
Start the transaction on the same connection your query will run on.