1

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. )

2 Answers 2

0

I believe it is because of the new line at the end of the file.

You should check if it contains only a new line and skip the import.

Sign up to request clarification or add additional context in comments.

1 Comment

I added if condition to UserImport.php if($row[0] !=""){ return new User([ 'name' =>$row[0], 'email'=>$row[1], 'password' => Hash::make('password'), ]); }
0

set if condition for UserImport.php

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.