0

I'm using laravel excel package to upload and use excel files with laravel. When I upload a file with unsupported format (E.g.:- .doc file) I get this exception.

Maatwebsite \ Excel \ Exceptions \ NoTypeDetectedException No message

But instead I need to use a flash message like this.

flash("Sorry you are using a wrong format to upload files.")->error();
return Redirect::back();

This is my code.

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

This is my import file

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToModel;

class MyImport implements ToModel
{

    public function model(array $row)
    {
       ...
    }
}
vimuth
  • 5,064
  • 33
  • 79
  • 116

1 Answers1

2

Import the exception at the top of your file:

use Maatwebsite\Excel\Exceptions\NoTypeDetectedException;

And use a try-catch block to catch the exception:

try {
    Excel::import(new MyImport, $file);
} catch (NoTypeDetectedException $e) {
    flash("Sorry you are using a wrong format to upload files.")->error();
    return Redirect::back();
}
D Malan
  • 10,272
  • 3
  • 25
  • 50