DB::beginTransaction(); After it is turned on, only the first piece of data can be inserted each time, and the rest will be rolled back.
code show as below:
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());
}
If you close the transaction, you can insert multiple pieces of data
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());
}
Please help me solve this problem, I am a newbie.
try
Error message, table structure, diagram.