ErrorException Undefined array key 7 on Laravel Excel when import or upload excel file

I just doing project import excel using laravel.
But there is an error when import / upload file excel and for web page can open and work.

ErrorException
Undefined array key 7

  public function model(array $row)

{

    return new Dso([

        'id_dso' => $row[1],

        'id_rso' => $row[2],

        'id_focus' => $row[3],

        'id_wilayah' => $row[4],

        'id_grup_wilayah' => $row[5],

        'nama_dso' => $row[6],

        'status' => $row[7],

    ]);

}

and my table on database format is
+-----------------+---------------------+------+-----+---------------------+-------------------------------+
| Field           | Type                | Null | Key | Default             | Extra                         |
+-----------------+---------------------+------+-----+---------------------+-------------------------------+
| id              | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment                |
| id_dso          | bigint(20) unsigned | NO   |     | NULL                |                               |
| id_rso          | bigint(20) unsigned | NO   |     | NULL                |                               |
| id_focus        | bigint(20) unsigned | NO   |     | NULL                |                               |
| id_wilayah      | bigint(20) unsigned | NO   |     | NULL                |                               |
| id_grup_wilayah | bigint(20) unsigned | NO   |     | NULL                |                               |
| nama_dso        | varchar(255)        | NO   |     | NULL                |                               |
| created_by      | varchar(255)        | NO   |     | NULL                |                               |
| created_date    | timestamp           | NO   |     | current_timestamp() | on update current_timestamp() |
| modified_by     | varchar(255)        | NO   |     | NULL                |                               |
| modified_date   | timestamp           | YES  |     | NULL                |                               |
| status          | tinyint(1)          | NO   |     | NULL                |                               |
| created_at      | timestamp           | YES  |     | NULL                |                               |
| updated_at      | timestamp           | YES  |     | NULL                |                               |
+-----------------+---------------------+------+-----+---------------------+-------------------------------+
14 rows in set (0.009 sec)

and my data sample is

Import data Sample

I’m using laravel 8.6 and my datbase is MariaDb.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Please start your array $row index from $row[0] instead of $row[1] as given as under

public function model(array $row)

{

    return new Dso([

        'id_dso' => $row[0],

        'id_rso' => $row[1],

        'id_focus' => $row[2],

        'id_wilayah' => $row[3],

        'id_grup_wilayah' => $row[4],

        'nama_dso' => $row[5],

        'status' => $row[6],

    ]);

}

Try this.

Method 2

Array starts with index 0.

So start from $row[0] until $row[6]

The key status will be $row[6] in your case.

public function model(array $row)
{
    return new Dso([
        'id_dso' => $row[0],
        'id_rso' => $row[1],
        'id_focus' => $row[2],
        'id_wilayah' => $row[3],
        'id_grup_wilayah' => $row[4],
        'nama_dso' => $row[5],
        'status' => $row[6],
    ]);
}

Method 3

try this

public function model(array $row)
    
    {
    
        return new Dso([
    
            'id_dso' => $row[1],
    
            'id_rso' => $row[2],
    
            'id_focus' => $row[3],
    
            'id_wilayah' => $row[4],
    
            'id_grup_wilayah' => $row[5],
    
            'nama_dso' => $row[6],
    
            'status' => ($row[7]) ? $row[7] : ' ',
    
        ]);
    
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x