DB::beginTransaction();開啟以後,每次都只能插入最開始的一條數據,其它的都被回滾了。
程式碼如下:
try {
DB::beginTransaction();
foreach ($rightOrWrongRows as $row) {
$question = array_combine($rightOrWrongHeader, $row);
RightOrWrong::create([
'question' => $question['question'],
'answer' => $question['answer'],
'scope' => $question['scope'],
'degree' => 1,
'exam_id' => $this->examId,
]);
}
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
Log::info($exception->getMessage());
}
如果關閉事務,那麼就可以插入多條資料
try {
//DB::beginTransaction();
foreach ($rightOrWrongRows as $row) {
$question = array_combine($rightOrWrongHeader, $row);
RightOrWrong::create([
'question' => $question['question'],
'answer' => $question['answer'],
'scope' => $question['scope'],
'degree' => 1,
'exam_id' => $this->examId,
]);
}
//DB::commit();
} catch (\Exception $exception) {
//DB::rollBack();
Log::info($exception->getMessage());
}
請各位朋友幫忙解決一下,本人菜鳥。
嘗試
雷雷錯誤訊息, 表結構, 圖.