laravel如何实现批量插入

说明

总所周知laravel 批量更新复杂的数据就需要通过json传递数据

<?php

try {
    $str = '[{"pageid":"270"},{"que":[{"title":"sad","type":"1",
    "option_a":"sad","option_b":"sad","option_c":"dad","option_d":"ad",
    "option_e":"d","option_f":"da","option_g":"sda","option_h":"d",
    "answer":"sdad"},{"title":"sdaf","type":"1","option_a":"sfa",
    "option_b":"fa","option_c":"da","option_d":"d","option_e":"d",
    "option_f":"dd","option_g":"ad","option_h":"add","answer":"ad"},
    {"title":"wedwq","type":"1","option_a":"ea","option_b":"aaas","option_c"
    :"ssssdas","option_d":"ad","option_e":"ad","option_f":"adad",
    "option_g":"wdqd","option_h":"ws","answer":"ada"},
    {"title":"dwq","type":"1","option_a":"ad","option_b":"ad",
    "option_c":"dad","option_d":"dad","option_e":"ad",
    "option_f":"dw","option_g":"ad","option_h":"sad","answer":"d"},
    {"title":"af","type":"1","option_a":"sad","option_b":"a","option_c":"af",
    "option_d":"saf","option_e":"asfdf","option_f":"da",
    "option_g":"fs","option_h":"dasf","answer":"afa"}]}]';
    $info = json_decode($str,true); # 将接受过来的json转化为数组
    $arr = $info[1];
    $value = '';
    foreach ($arr as $k => $value) {
        $value = $value;
    }
    $pageid = $info[0]->pageid;
    foreach ($value as $info) {
        $arr1 = [
            'title' => $info->title,//题目
            'type' => $info->type,//题目类型
            'option_a' => $info->option_a,//A
            'option_b' => $info->option_b,//B
            'option_c' => $info->option_c,//C
            'option_d' => $info->option_d,//D
            'option_e' => $info->option_e,//E
            'option_f' => $info->option_f,//F
            'option_g' => $info->option_g,//G
            'option_h' => $info->option_h,//H
            'answer' => $info->answer,//正确答案
            'pageid' => $pageid,
        ];
        \DB::table('v')->insert($arr1);

    }
} catch (\Exception $exception) {
    $exception->getMessage();
}
  • 对应数据库结构

CREATE TABLE `v` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL,
  `type` int(1) DEFAULT NULL,
  `option_a` varchar(100) DEFAULT NULL,
  `option_b` varchar(100) DEFAULT NULL,
  `option_c` varchar(100) DEFAULT NULL,
  `option_d` varchar(100) DEFAULT NULL,
  `option_e` varchar(100) DEFAULT NULL,
  `option_f` varchar(100) DEFAULT NULL,
  `option_g` varchar(100) DEFAULT NULL,
  `option_h` varchar(100) DEFAULT NULL,
  `answer` varchar(100) DEFAULT NULL,
  `score` int(11) DEFAULT NULL,
  `pageid` int(11) DEFAULT NULL,
  `token` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `token` (`token`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=105 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='知识竞赛表';

优化代码版本二

  public function store(Request $request)
     {
        $str=$request->except('_token');
         $str=implode($str);//将数组转化为字符串(由于传输过来的是单个数组所以只能转化为字符串进行操作)
         $info = json_decode($str);//转换为json数组
         $arr = $info[1];//得到数组第一小数组个信息
         $value='';//下面循环将第二个数组的首字符去掉然后保存为新的数组
         foreach ($arr as $k=>$value) {
             $value=$value;
         }
         $pageid=$info[0]->pageid;//得到id
         $type_num=PolicePage::find($pageid);
         $type_num=$type_num->page_type;
         switch ($type_num){
             case  0:
                 foreach ($value as  $info){
                     $arr1 = [
                         'title' => $info->title,//题目
                         'type' => $info->type,//题目类型
                         'option_a' => $info->option_a,//A
                         'option_b' => $info->option_b,//B
                         'option_c' => $info->option_c,//C
                         'option_d' => $info->option_d,//D
                         'option_e' => $info->option_e,//E
                         'option_f' => $info->option_f,//F
                         'option_g' => $info->option_g,//G
                         'option_h' => $info->option_h,//H
                         'answer' => $info->answer,//正确答案
                         'pageid'=>$pageid,
                     ];
                     $data=PoliceExam::create($arr1);
                 }
                 break;
             case  1:
                 foreach ($value as  $info){
                     $arr1 = [
                         'title' => $info->title,//题目
                         'answer' => $info->answer,//正确答案
                         'pageid'=>$pageid,
                     ];
                     $data=PoliceExam::create($arr1);
                 }
                 break;
             case  2:
                 foreach ($value as  $info){
                     $arr1 = [
                         'title' => $info->title,//题目
                         'type' => $info->type,//题目类型
                         'option_a' => $info->option_a,//A
                         'option_b' => $info->option_b,//B
                         'option_c' => $info->option_c,//C
                         'option_d' => $info->option_d,//D
                         'option_e' => $info->option_e,//E
                         'answer' => $info->answer,//正确答案
                         'pageid'=>$pageid,
                     ];
                     $data=PoliceExam::create($arr1);
                 }
                 break;
         }
 
         if($data){
             return ResponseLayout::apply(true,'数据录入成功',['data'=>$data]);
         }else{
             return ResponseLayout::apply(false,'数据录入失败',['data'=>$data]);
         }
     }
posted @ 2020-03-12 22:49  以己为镜  阅读(11115)  评论(0编辑  收藏  举报