laravel 5.6 bulk inserting json data

后端 未结 5 837
無奈伤痛
無奈伤痛 2021-01-05 14:21

I am trying to build an API to store and retrieve MCQ exam papers. I am using laravel resource class to send handle Json data. I need to insert 40 records into MySQL databas

5条回答
  •  囚心锁ツ
    2021-01-05 14:31

    This code works for me. It inserted all 40 records with no problems.

    $array = $request->all();
        foreach($array["data"] as $row)
        {
            Exam_Paper::create(['paper_id'      => $row["paper_id"],
                                'question_no'   => $row["question_no"],
                                'question'      => $row["question"],
                                'answer1'       => $row["answer1"],
                                'answer2'       => $row["answer2"],
                                'answer3'       => $row["answer3"],
                                'answer4'       => $row["answer4"],
                                'answerC'       => $row["answerC"],
                                'knowarea'      => $row["knowarea"],
            ]);
    
        }
    

提交回复
热议问题