简体   繁体   English

Laravel将excel文件导入数据库

[英]Laravel import excel file to database

When I import the excel file to the database, the file imported to the database but it inserts together with the excel first row which is the column name, I don't know how to make it upload only the data start from the second row. 当我将excel文件导入数据库时​​,该文件导入到数据库但它与excel第一行(列名称)一起插入,我不知道如何使其仅从第二行开始上传数据。

Below is the code that I use in the controller to import 下面是我在控制器中导入的代码

public function import()
{
    Excel::import(new StudentImport,request()->file('file'));

    return redirect('/admin/dashboard')->with('flash_message_success','Upload successful');
}

I expected the data inserted to the database starting from the second row and skip the first row which is the column name. 我期望从第二行开始插入数据库并跳过第一行,即列名。

You need to specify the headingRow 您需要指定headingRow

 public function headingRow(): int
 {
    return 1;
 }

and your import class will be 你的导入类将是

class StudentImport implements ToModel, WithHeadingRow{}

Refer docs 参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM