'Lumen/Eloquent transaction rollback is not working
I have this function that permit to save in 4 tables.
I have an error at that line $stade->save()
( it's normal it's a test ) so it should not save the club $club->save()
that is 4 lines before.
But I don't understand why, the rollback() in the catch is not doing is job and I still have a record in club table. And yes I receive the 409 error so the application is passing by the catch and so by the DB::rollback()
Can someone help me please to understand why the rollback is not working ?
DB::beginTransaction();
try {
$club = new Club();
$club->login = $request->input('login');
$club->email = $request->input('email');
$plainPassword = $request->input('password');
$club->password = app('hash')->make($plainPassword);
$club->nom_equipe = $request->input('nom_club');
$club->nation_id = $request->input('nation_id');
$club->argent = $this->argentDepart;
$club->save();
$stade = new Stade();
$stade->club_id = $club->id;
$stade->save();
$installation = new Installations();
$installation->club_id = $club->id;
$installation->save();
$compositionTactique = new CompositionTactique();
$compositionTactique->club_id = $club->id;
$compositionTactique->save();
DB::commit();
//return successful response
return response()->json(['user' => $club->nom_equipe, 'message' => 'CREATED'], 201);
} catch (\Exception $e) {
DB::rollback();
//return error message
return response()->json(['message' => $e], 409);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|