DB::beginTransaction(); Selepas ia dihidupkan, hanya sekeping data pertama boleh dimasukkan setiap kali, dan selebihnya akan digulung semula.
Kod adalah seperti berikut:
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());
}
Jika anda menutup transaksi, anda boleh memasukkan beberapa keping 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());
}
Tolong bantu saya selesaikan masalah ini, saya masih baru.
cuba
Mesej ralat, struktur jadual, rajah.