0

Trying to import excel data to my mysql db and getting ErrorException Undefined offset: 0.

Tried using var_dump($row) to check the array of the row of the data. It looks off and not sure how to make the data shows nicely so that able to pass to db.

array(5) { ["a"]=> string(1) "B" ["white"]=> string(5) "Green" ["example1_at_gmailcom"]=> string(18) "example2@gmail.com" [60123456789]=> int(60162313142) [5]=> int(5) }

This is my excel data:

enter image description here

Model

 public function model(array $row)
{
    var_dump($row);
    return new ContactList([
        'first_name' => $row[0],
        'last_name' => $row[1],
        'email' => $row[2],
        'phone' => $row[3],
        'group_id' => $row[4],
    ]);
}

I already tried using replacing the $row[] with string and it works perfectly storing the data into my db.

Controller

if($request->hasFile('selected_file')){

            $file = $request->file('selected_file');
            Excel::import(new ContactListsImport, $file);


            return redirect()->back()->with('success', 'Data saved successfully!');
        }
N.Tec
  • 127
  • 2
  • 13
  • 1
    you are dumping the array you are getting ... do you see a key `0` in it? – lagbox Oct 10 '20 at 08:45
  • sry, i am new to this. No i didnt see it but even i removed the vardump i still getting the same error ErrorException Undefined offset: 0 – N.Tec Oct 10 '20 at 08:47
  • because there is no index `0` as you can see as you dumped the array and there is no key `0` ... you can't access keys of an array that don't exist ... you have an issue here as the first row of this file is being used as the headers (column names), you should look into the documentation to adjust this or not use this behavior – lagbox Oct 10 '20 at 08:48
  • Just an FYI, but Laravel using Symfony's VarDumper component so you can use [dump()](https://laravel.com/docs/8.x/helpers#method-dump) and [dd()](https://laravel.com/docs/8.x/helpers#method-dd) for a more readable output. – Rwd Oct 10 '20 at 08:49
  • What version of Laravel and Laravel Excel are you using? – Rwd Oct 10 '20 at 08:49
  • 6.18.35 and 3.1 – N.Tec Oct 10 '20 at 08:54
  • thanks using dd() the array looking great but it still getting error – N.Tec Oct 10 '20 at 08:56
  • Yeah, I didn't think `dd()` would solve anything :) Please can you show all the code in your `ContactListsImport` class. – Rwd Oct 10 '20 at 09:00
  • I already found the way of doing it. Thks to lagbox's hint. Also thanks for letting me know using dd() for readable output – N.Tec Oct 10 '20 at 09:02
  • previously no and then i added header for column name mentioned by lagbox. So is the problem with the WithHeadingRow ? – N.Tec Oct 10 '20 at 09:05

1 Answers1

1

You need to remove the WithHeadingRow interface from your Import class to use numeric indexes for the array.

As per the documentation, when your Import class implements WithHeadingRow it will use the first row for the indexes of the array and in turn remove it from the rows that are provided.

Rwd
  • 34,180
  • 6
  • 64
  • 78