dovgqc2648 2018-04-19 16:42
浏览 80
已采纳

通过csv将大量项目上传到数据库

I am trying to upload number of students to a database via a csv file. I am able to upload a database to the csv file, however it is only uploading the first student to the database and not the rest. Any thoughts on what is going wrong?

Function which uploads the csv file to the database:

function csvstudents(Request $req) {
    if ($req->hasFile('csvfile')) {
        $path = $req->file('csvfile')->getRealPath();
        $data = \Excel::load($path)->get();

        if ($data->count()) {
            foreach ($data as $key => $value) {
                $arr = [
                    'studentID' => $value->studentID, 
                    'name' => $value->name,
                    'var1' => $value->var1,
                    'var2' => $value->var2,
                    'email' => $value->email,
                    'address1' => $value->address1,
                    'c1' => $value->c1,
                    'c2' => $value->c2,
                    'c3' => $value->c3,
                    'c4' => $value->c4,
                    'c5' => $value->c5,
                    'c6' => $value->c6,
                    'c7' => $value->c7,
                    'c8' => $value->c8,
                    'c9' => $value->c9,
                    'c10' => $value->c10,

                ];

            }
            if (!empty($arr)) {
                DB::table('students')->insert($arr);
                return "Success";
            }
        }
    }
}

Test CSV file:

9   Katy Perry  A*  WellDone!   katy@hotmail.com    91 Fromans Road, Moseley, Birmingham, B94TJ 4   4   5   6   7   6   5   4   2   2

10  chut winder A*  WellDone!   katy@hotmail.com    92 Fromans Road, Moseley, Birmingham, B94TJ 4   4   5   6   7   6   5   4   2   2

11  Test        A*  WellDone!   katy@hotmail.com    93 Fromans Road, Moseley, Birmingham, B94TJ 4   4   5   6   7   6   5   4   2   2

So f I was to upload the test database it would only insert the first student in to the database only, I want to upload all 3 entries.

  • 写回答

2条回答 默认 最新

  • doufu5521 2018-04-19 16:56
    关注

    You need to either insert inside the loop or push to $arr and insert outside the loop

    Inside loop

    if ($data->count()) {
        foreach ($data as $key => $value) {
            $arr = ['studentID' => $value->studentID, 
                'name' => $value->name,
                'var1' => $value->var1,
                'var2' => $value->var2,
                ...
            ];
    
            DB::table('students')->insert($arr);
        }
        return "Success";
    }
    

    Outside loop

    if ($data->count()) {
        $arr = [];
        foreach ($data as $key => $value) {
            $arr[] = ['studentID' => $value->studentID, 
                'name' => $value->name,
                'var1' => $value->var1,
                'var2' => $value->var2,
                ...
            ];
        }
        if (! empty($arr)) {
            DB::table('students')->insert($arr);
            return "Success";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失