Not answering your question, but you never need to use a transaction when only updating one table.
Apr 8, 2024
9
Level 1
Laravel transactions not working rollback
I would like to know why the rollback doesn't work? From what I understood, the user was only supposed to be created if there was no error. But my current code creates the user even if it gives the simulated error, could anyone help me?
public function Teste() { DB::beginTransaction();
try {
$Cliente = Cliente::create([
'Nome' => 'Teste',
'Email' => '[email protected]',
'CPF' => '12345678901',
'Telefone' => '11999999999',
'Data_de_Nascimento' => '1990-01-01',
'Verificado' => true
]);
throw new \Exception('Erro simulado');
DB::commit();
return 'Transação bem-sucedida';
} catch (\Exception $e) {
DB::rollBack();
return 'Erro na transação: ' . $e->getMessage();
}
}
Please or to participate in this conversation.