0

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.

1
  • is $row an array with 7 items ? in this case it is 0-indexed ($row[0] ... $row[6]) Commented Sep 15, 2021 at 14:04

3 Answers 3

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.

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

Comments

0

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

1 Comment

I'm sorry is my explanation is unclear. I mean is happen when I import excel, for web page is can open
0

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

1 Comment

I was tried, but still same error. This happen when import excel document for web page is okay no problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.