Rumah  >  Soal Jawab  >  teks badan

php - Mengapa laravel hanya memasukkan satu keping data setiap kali?

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.

阿神阿神2702 hari yang lalu877

membalas semua(2)saya akan balas

  • 習慣沉默

    習慣沉默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());
        }

    balas
    0
  • 世界只因有你

    世界只因有你2017-06-19 09:09:10

    Mesej ralat, struktur jadual, rajah.

    balas
    0
  • Batalbalas