laravel 5.6 bulk inserting json data



PHP Snippet 1:

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

    }

PHP Snippet 2:

{
  "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);
}

PHP Snippet 3:

$data = array(
    array('name'=>'Coder 1', 'rep'=>'4096'),
    array('name'=>'Coder 2', 'rep'=>'2048'),
    //...
);

Coder::insert($data);

PHP Snippet 4:

$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
  }
}

PHP Snippet 5:

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']);
  }
}         

}

PHP Snippet 6:

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


            foreach ($newModelsArray as $objectItr) {

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

PHP Snippet 7:

collect([ 'x' => 3 ])->toJson();