I am using Laravel 5.3, and have function for saving data, but for some reason rollback is not working. What should I change or what is the proper way to make transactions. I am pretty new to this stuff so I am not sure about the right way.
protected function addKSMembers(Request $request, Application $application, $formName){
//validation stuff
if(!$this->testRentType($request->input('type')))
return false;
if(!$this->testCompetitionLevel($request->input('level')))
return false;
try{
DB::beginTransaction();
$sport = Sports::where('id', $request->input('sport'))->first();
$ksMember= FormKSMembers::firstOrNew([
'application' => $application->id,
'unique' => $request->input('unique'),
]);
$status = $ksMember->exists;
$ksMember->sport_category = $sport->sport;
$ksMember->rent_type = $request->input('type');
$ksMember->sport_type = $sport->sport_type;
$formResult = $this->getFormResultByString($formName, $application->tender->id, $application->id);
$this->addCoaches($request->input('coach'), $application->id, $formResult->id, $ksMember->unique);
$ksMember->save();
DB::commit();
} catch(\Exception $e){
DB::rollback();
}
if($status)
$this->extra = "OK";
return true;
}
protected function addCoaches(array $coaches, $appID, $formResultID, $resultTableID){
if(count($coaches) == 0)
return;
foreach($coaches as $coach){
Coaches::create([
'coach' => $coach['ime'],
'application' => $appID,
'form_result' => $formResultID,
'resultTable' => $resultTableID,
'education' => $coach['education'],
]);
}
return;
}
via Tim