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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制