码迷,mamicode.com
首页 > 其他好文 > 详细

laravel如何实现批量插入

时间:2020-03-12 23:17:29      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:答案   正确答案   批量   div   val   tab   eid   table   sage   

1.自己写的做个记录

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);
            $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();
        }

 

laravel如何实现批量插入

标签:答案   正确答案   批量   div   val   tab   eid   table   sage   

原文地址:https://www.cnblogs.com/yaoliuyang/p/12483437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!