excellaravelexcel-import

Import excel file to database using Laravel


why get undefined offset when importing excel file to the database using laravel.

UserImport.php

public function model(array $row)
{
    var_dump($row);
    
    return new User([
        'name' =>$row[0],
        'email'=>$row[1],
        'password' => Hash::make('password'),

    ]);
}

UserImportController

public function store(Request $request){

    $file = $request->file('file');
    Excel::import(new UsersImport, $file);
    return back()->withStatus('Successfully');
}

when uploading the excel file, display it like this. enter image description here

I used var_dump() to see the array. I entered 4 rows in the excel file. But display 5 array data. Why that??? (display in entered image. )


Solution

  • set if condition for UserImport.php

    if($row[0] !=""){ 
        return new User([ 
            'name' =>$row[0], 
            'email'=>$row[1], 
            'password' => Hash::make('password'), 
       ]); 
    }