Home > Back-end >  ErrorException Undefined array key 7 on Laravel Excel when import or upload excel file
ErrorException Undefined array key 7 on Laravel Excel when import or upload excel file

Time:09-16

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.

CodePudding user response:

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.

CodePudding user response:

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] : ' ',
    
        ]);
    
    }

CodePudding user response:

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],
    ]);
}
  • Related