1

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.

2 Answers 2

4

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

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

Comments

0

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.

3 Comments

It worked! but may I ask another thing? what does this mean? "Add [resident_fname] to fillable property to allow mass assignment on [App\Resident]."
laravel.com/docs/5.8/eloquent Check the mass assignment section, Laravel models have a fillable attribute you have to set in order to perform some operations on them.
it worked! Thanks for the Help! It meant a lot! Have a great day ahead! :)

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.