1

Is there a way i can stop the reader from reading the excel file when any of the row data is different? When i upload an excel file, i get Undefined index: description which means description cannot be found in the file uploaded.

Is there a way i can just handle this error ?

if ($request->file('imported-file')) {
  $path = $request->file('imported-file')->getRealPath();

  $data = Excel::load($path, function($reader) {
     $reader->calculate(false);
  })->get();

  if (($request->file('imported-file')->getClientOriginalExtension()) != 'xlsx') {
    return redirect('')->with('error','File Format may not be supported');
  } else {

    if (!empty($data) && $data->count()) {
      foreach ($data->toArray() as $row) {
          if (!empty($row)) {
            $dataArray[] = [
              'name' => $row['name'],
              'description' => $row['description'],
            ];
          } 
      }

      if (!empty($dataArray)) {
         Item::insert($dataArray);

         return redirect('')->with('status','successfully added');  
      }
    }

  }
}
1
  • if (!$row['description']) {break;} Commented Dec 11, 2017 at 0:40

2 Answers 2

1

Instead of:

'description' => $row['description'],

you could use

'description' => array_get($row, 'description'),
Sign up to request clarification or add additional context in comments.

Comments

0

I was also facing the same issue, here is my solution, Hope it will help someone.

Note : This is for Laravel 6.0, Importer class must implements ToModel, WithHeadingRow these two interfaces,

return new Holiday([
'name'          => Arr::get($row,'name'),
'start_date'    => Arr::get($row,'start_date'),
'end_date'      => Arr::get($row,'end_date'),
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.