laravel 5.6批量插入json数据

时间:2018-02-19 07:42:36

标签: php mysql json laravel bulkinsert

我正在尝试构建一个API来存储和检索MCQ考试试卷。我正在使用laravel资源类来发送句柄Json数据。我需要在一个查询中将40条记录插入MySQL数据库,而不使用多维数组。有没有可用的方法?

前端的示例数据:

{
"data":[
    {
    "paper_id":"5",
    "question_no":"2",
    "question":"test insert code",
    "answer1":"answer1",
    "answer2":"answer2 ",
    "answer3":"answer3 ",
    "answer4":"Answer4 ",
    "answerC":"Correct Answer",
    "knowarea":"who knows!"

    },

    {
        "paper_id":"5",
        "question_no":"3",
        "question":"test insert code",
        "answer1":"answer1",
        "answer2":"answer2 ",
        "answer3":"answer3 ",
        "answer4":"Answer4 ",
        "answerC":"Correct Answer",
        "knowarea":"who knows!"

    },

    {
        "paper_id":"5",
        "question_no":"4",
        "question":"test insert code",
        "answer1":"answer1",
        "answer2":"answer2 ",
        "answer3":"answer3 ",
        "answer4":"Answer4 ",
        "answerC":"Correct Answer",
        "knowarea":"who knows!"

    },

    {
        "paper_id":"5",
        "question_no":"5",
        "question":"test insert code",
        "aanswer1":"answer1",
        "answer2":"answer2 ",
        "answer3":"answer3 ",
        "answer4":"Answer4 ",
        "answerC":"Correct Answer",
        "knowarea":"who knows!"

    }
]

}`

前端发送40个这样的对象,我需要将它们批量插入到我的数据库中。 以下代码是我的控制器存储功能,

`   $paper->paper_id = $request->input('paper_id');
    $paper->question_no = $request->input('question_no');
    $paper->question = $request->input('question');
    $paper->answer1 = $request->input('answer1');
    $paper->answer2 = $request->input('answer2');
    $paper->answer3 = $request->input('answer3');
    $paper->answer4 = $request->input('answer4');
    $paper->answerC = $request->input('answerC');
    $paper->knowarea = $request->input('knowarea');

    if($paper->save())
    {
        return new ExamPaperResource($paper);
    }

批量插入数据有哪些选择?

5 个答案:

答案 0 :(得分:3)

根据您的示例数据,您可以json_decode数据,然后使用单个Model::insert()

{
  "data":[
    {
      "paper_id":"5",
      "question_no":"2",
      "question":"test insert code",
      "answer1":"answer1",
      "answer2":"answer2 ",
      "answer3":"answer3 ",
      "answer4":"Answer4 ",
      "answerC":"Correct Answer",
      "knowarea":"who knows!"
    },
    ...
  ]
}

// Controller.php
public function store($json)
{
    $data = json_decode($json, true);
    Paper::insert($data);
}

这将从你的json创建数组,然后一次插入所有记录。

答案 1 :(得分:1)

您可以使用:Eloquent :: insert()

如下图所示。

noexcept

答案 2 :(得分:1)

尝试以下代码

$jsonarray =json_decode(json_encode($b),TRUE); // $b=your json array
foreach ($jsonarray as $key => $value) 
{
 foreach ($value as $a => $b) 
  {
  $qry=DB::insert('insert into your_table(colomn_name1,colomn_name2)values(?,?)',[$b['indexname1'],$b['indexname2']]); //index name will be paper_id,question_no etc
  }
}

您的代码将如下所示

public function bulkdata(Request $request)
  {

   $b=$request->input('data');
$jsonarray =json_decode(json_encode($b),TRUE);
foreach ($jsonarray as $key => $value) 
{

 foreach ($value as $a => $b) 
  {

  $qry=DB::insert('insert into yourtable(paper_id,question_no,question,answer1,answer2,answer3,answer4,answerC,knowarea)values(?,?,?,?,?,?,?,?,?)',[$b['paper_id'],$b['question_no'],$b['question'],$b['answer1'],$b['answer2'],$b['answer3'],$b['answer4'],$b['answerC']$b['knowarea']);
  }
}         

}

答案 3 :(得分:0)

此代码适用于我。它插入了所有40条记录,没有任何问题。

$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"],
        ]);

    }

答案 4 :(得分:0)

插入带有更新模型数组字段的选择查询

$newModelsArray=ModelTable::where(....)->get();


            foreach ($newModelsArray as $objectItr) {

            $newObjectItr = $objectItr->replicate();
            $newObjectItr->field=newValue;
            $newObjectItr->save();
            }

,然后您将在那里更新并保存到表中(将其克隆回数据库) -> replicate()将克隆modelObject,而-> save()将其添加到循环内的数据库中!

谢谢 阿里