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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分