laravel 5.6 批量插入 json 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48861486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.6 bulk inserting json data
提问by Naveed Sheriffdeen
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 database in a single query without using multi dimensional arrays. Is there any method available?
我正在尝试构建一个 API 来存储和检索 MCQ 试卷。我正在使用 laravel 资源类来发送句柄 Json 数据。我需要在不使用多维数组的情况下在单个查询中将 40 条记录插入到 MySQL 数据库中。有没有可用的方法?
Sample data from front end:
来自前端的示例数据:
{
"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!"
}
]
}`
}`
The front end send 40 such objects i need to bulk insert them into my database. The below code is my controller store function,
前端发送 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);
}
What are my choices for bulk inserting the data?
我有哪些批量插入数据的选择?
采纳答案by Naveed Sheriffdeen
This code works for me. It inserted all 40 records with no problems.
这段代码对我有用。它插入了所有 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"],
]);
}
回答by Naveed Sheriffdeen
Based on your sample data, you can json_decode
the data and then use a single Model::insert()
:
根据您的示例数据,您可以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);
}
That will create arrays from your json and then insert all the records at once.
这将从您的 json 创建数组,然后一次插入所有记录。
回答by syed mahroof
Try code below
试试下面的代码
$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
}
}
your code will look like this
你的代码看起来像这样
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']);
}
}
}
回答by Virk
You can use: Eloquent::insert()
您可以使用:Eloquent::insert()
Like example below.
像下面的例子。
$data = array(
array('name'=>'Coder 1', 'rep'=>'4096'),
array('name'=>'Coder 2', 'rep'=>'2048'),
//...
);
Coder::insert($data);
回答by Ali Bayati
insert select query with update models array field
使用更新模型数组字段插入选择查询
$newModelsArray=ModelTable::where(....)->get();
foreach ($newModelsArray as $objectItr) {
$newObjectItr = $objectItr->replicate();
$newObjectItr->field=newValue;
$newObjectItr->save();
}
and there you'll update and save in to the table (clone it back to the database) ->replicate() will clone the modelObject and the ->save() will add it to the database inside the loop !
然后您将更新并保存到表中(将其克隆回数据库)->replicate() 将克隆模型对象,->save() 将其添加到循环内的数据库中!
Thanks Ali
谢谢阿里