laravel 5.6 bulk inserting json data
Based on your sample data, you can json_decode
the data and then use a single 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.