laravel excel 添加闪现消息而不是 NoTypeDetectedException

laravel excel add flash message instead of NoTypeDetectedException

我正在使用 laravel excel 包上传 excel 个文件并使用 laravel。当我上传格式不受支持的文件(例如:- .doc 文件)时,出现此异常。

Maatwebsite \ Excel \ Exceptions \ NoTypeDetectedException No message

但是我需要使用像这样的闪现消息。

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

这是我的代码。

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

这是我的导入文件

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToModel;

class MyImport implements ToModel
{

    public function model(array $row)
    {
       ...
    }
}

在文件顶部导入异常:

use Maatwebsite\Excel\Exceptions\NoTypeDetectedException;

并使用 try-catch 块来捕获异常:

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