Arato
Arato

Reputation: 25

Laravel 5.7 Import Excel File: Undefined offset

I'm trying to import an excel file and save it into the database but I get the error and I don't know what this means, I'm new to Laravel though.

ErrorException (E_NOTICE) Undefined offset: 14

Here's my code for the Model:

public function model(array $row)
{
    return new Resident([
        'resident_fname' => $row[1],
        'resident_lname' => $row[2],
        'resident_mi'    => $row[3],
        'resident_dob'   => $row[4],
        'role'           => $row[5],
        'resident_age'   => $row[6],
        'resident_address'  => $row[7],
        'resident_contact'  => $row[8],
        'resident_email'    => $row[9],
        'resident_purok'    => $row[10],
        'resident_status'   => $row[11],
        'resident_gender'   => $row[12],
        'resident_religion' => $row[13],
        'ResidentVoter_status'  => $row[14],
    ]);

Here's my code for my controller:

public function import(Request $request)
{
    $import = Excel::import(new ResidentImport, request()->file('import_file'));
    dd($import);
    return view('pages.residents')->with('success', 'Imported Successfully');
}

this is the code for my File button:

<form action="{{ url('/import') }}" method="POST" enctype="multipart/form-data"></a>
        {{ csrf_field() }}
            <input type="file" name="import_file" style="direction: rtl;"></input>
        <button type="submit" name="upload" class="btn btn-success">Submit</button></form>

while here is my routes:

Route::post('/import', 'ImportController@import');

Can anyone help me with this? I really don't have any idea with what to do with this error.

Upvotes: 1

Views: 4800

Answers (2)

Awais
Awais

Reputation: 117

Put @ before row variable. @$row[14] . It will ignore if row variable is undefined.

Upvotes: 4

namelivia
namelivia

Reputation: 2735

The error menans that the row array does not have a 14th position. Arrays start at 0 so instead of 1 to 14 access 0 to 13 on the model.

Upvotes: 0

Related Questions