Search code examples
phplaravellaravel-5.5laravel-5.6laravel-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.

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.


Solution

  • You need to specify the headingRow

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

    and your import class will be

    class StudentImport implements ToModel, WithHeadingRow{}
    

    Refer docs