laravel 5.6批量插入json数据
Posted
技术标签:
【中文标题】laravel 5.6批量插入json数据【英文标题】:laravel 5.6 bulk inserting json data 【发布时间】:2018-07-29 09:53:06 【问题描述】:我正在尝试构建一个 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);
对于批量插入数据,我有哪些选择?
【问题讨论】:
使用 eloquent insert 方法进行批量上传 和the posts that are shown in this search 都不相关? 那些帖子很有用@chb,但我正在寻找基于 laravel 的解决方案。像 eloquent 或任何其他软件包或内置功能 【参考方案1】:根据您的样本数据,您可以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 创建数组,然后一次插入所有记录。
【讨论】:
我在运行 json_decodeType error: Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, null given
时得到这个错误
我成功了。您应该选择数组$json->data
,即数据的值。【参考方案2】:
你可以使用:Eloquent::insert()
如下例所示。
$data = array(
array('name'=>'Coder 1', 'rep'=>'4096'),
array('name'=>'Coder 2', 'rep'=>'2048'),
//...
);
Coder::insert($data);
【讨论】:
我需要一次插入 40 条记录,这种方法必须写入 40 个数组。我正在寻找更小的代码 不,我需要一次插入 40 条记录。如果我要将数据插入到您的方法中,则数据以 json 对象的形式来自前端,我不必写: `array('name'=> 'coder') 40 次。【参考方案3】:试试下面的代码
$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']);
【讨论】:
为我工作:)【参考方案4】:此代码对我有用。它插入了所有 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"],
]);
【讨论】:
【参考方案5】:插入带有更新模型数组字段的选择查询
$newModelsArray=ModelTable::where(....)->get();
foreach ($newModelsArray as $objectItr)
$newObjectItr = $objectItr->replicate();
$newObjectItr->field=newValue;
$newObjectItr->save();
然后您将更新并保存到表中(将其克隆回数据库) ->replicate() 将克隆模型对象,而 ->save() 将把它添加到循环内的数据库中!
谢谢 阿里
【讨论】:
以上是关于laravel 5.6批量插入json数据的主要内容,如果未能解决你的问题,请参考以下文章
PHP框架 Laravel Eloquent ORM 批量插入数据,怎么实现