php - Mengapa laravel hanya memasukkan satu keping data setiap kali?
阿神
阿神 2017-06-19 09:07:10
0
2
866

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.

阿神
阿神

闭关修行中......

membalas semua(2)
習慣沉默

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());
    }
世界只因有你

Mesej ralat, struktur jadual, rajah.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!