1

I have the following code to read a xlsx spreadsheet but this seems to have some issue:

Excel::load($filePath, function ($reader) use ($classId) {
        // Getting all results
        $reader->get();
        // Loop through all sheets
        $reader->each(function ($sheet) use ($classId) {
            // Loop through all rows
            $sheet->each(function ($row) use ($classId) {
                    if (filter_var($row, FILTER_VALIDATE_EMAIL)) {
                        $invitation = new Invitations();
                        $invitation->email = $row;
                        $invitation->status = 0;
                        $invitation->class_id = $classId;
                        $invitation->token = str_random(10);
                        $invitation->sent = 0;
                        $invitation->created_at = time();
                        $invitation->save();
                    }
            });
        });
    });

The problem with this code is not all the fields are being checked for ex: I have email address on field A5 and than I have another email address on D7 and so on... it only takes D7 value not A5... if I put all in one column let say B1,B2,B3 and so on it works fine. How can I make it work wherever there is an email address on any field and insert it into db.

Lulzim Fazlija
  • 865
  • 2
  • 16
  • 37

1 Answers1

0

I did resolved it by changing to this:

Excel::load($filePath, function ($reader) use ($classId) {
        // Getting all results
        $reader->get()->toArray();
        $reader->noHeading();
        $reader->ignoreEmpty();
        // Loop through all sheets
        $reader->each(function ($sheet) use ($classId) {
            // Loop through all rows
            $sheet->each(function ($row) use ($classId) {
                foreach ($row as $value) {
                    if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
                        $invitation = new Invitations();
                        $invitation->email = $value;
                        $invitation->status = 0;
                        $invitation->class_id = $classId;
                        $invitation->token = str_random(10);
                        $invitation->sent = 0;
                        $invitation->created_at = time();
                        $invitation->save();
                    }
                }
            });
        });
    });

And changed 'force_sheets_collection' => true in conf so I always have the same return where there is one sheet or more...

Lulzim Fazlija
  • 865
  • 2
  • 16
  • 37