Rumah > Soal Jawab > teks badan
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.
習慣沉默2017-06-19 09:09:10
cuba
try {
DB::beginTransaction();
$data = [];
foreach ($rightOrWrongRows as $row) {
$question = array_combine($rightOrWrongHeader, $row);
$data[] = [
'question' => $question['question'],
'answer' => $question['answer'],
'scope' => $question['scope'],
'degree' => 1,
'exam_id' => $this->examId,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
RightOrWrong::insert($data);
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
Log::info($exception->getMessage());
}