Todos usamos DB::transaction()para múltiples consultas de inserción. Al hacerlo, ¿se try...catchdebe colocar una dentro o envolverla? ¿Es incluso necesario incluir un try...catchcuándo una transacción fallará automáticamente si algo sale mal?
Ejemplo de try...catchenvoltura de una transacción:
// try...catch
try {
// Transaction
$exception = DB::transaction(function() {
// Do your SQL here
});
if(is_null($exception)) {
return true;
} else {
throw new Exception;
}
}
catch(Exception $e) {
return false;
}
Todo lo contrario, DB::transaction()envolver un intento ... captura:
// Transaction
$exception = DB::transaction(function() {
// try...catch
try {
// Do your SQL here
}
catch(Exception $e) {
return $e;
}
});
return is_null($exception) ? true : false;
O simplemente una transacción sin intentar ... atrapar
// Transaction only
$exception = DB::transaction(function() {
// Do your SQL here
});
return is_null($exception) ? true : false;