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

我只是使用 laravel 进行项目导入 excel。 但是导入/上传文件excel时出现错误,网页可以打开和工作。

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],

    ]);

}

我的 table 数据库格式是

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

我的数据样本是

Import data Sample

我正在使用 laravel 8.6,我的数据库是 MariaDb。

试试这个

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

数组以索引 0.

开头

所以从 $row[0] 开始直到 $row[6]

密钥 status 在您的情况下将是 $row[6]

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

请从 $row[0] 开始您的数组 $row 索引,而不是从 $row[1] 开始,如下所示

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],

    ]);

}

试试这个。