如何通过Laravel中的API捕获ShouldQueue导入上的失败和错误

问题描述

通过这些过程,我可以上传Big Excel文件中包含的数据,如果队列上存在任何验证问题,它将跳过并上传剩余文件。但是我不知道如何通过API获得那些验证错误。我尝试了The script is called: ex13.py Your first variable is: apple Your second variable is: orange Your third variable is: grapefruit 的方式来检查是否有我知道不会发生的错误。向我提出解决这些验证错误的最佳方法。

在我的ImportController上

if ($import->failures()->isNotEmpty())

在ApplicationImport上(使用Maatwebsite \ Excel)

public function import(Request $request)
{
    $rules = [
        'file' => 'required|mimes:xlsx,xls','type' => 'filled|string|not_in:null',];
    $validator = Validator::make($request->all(),$rules);
    if ($validator->fails()) {
        $errors = $validator->errors();
        return response()->json($errors,404);
    }
    
    $type = $request->input('type');
    $file = $request->file;
    $fileName = time() . '--u-' . auth()->user()->id . '.' .$file->extension();
    $location = $file->storeAs('importFile/applicationFile',$fileName);
    $import = new ApplicationImport;
    $import->import($location);
    if ($import->failures()->isNotEmpty()) {
        return response()->json(['response' => 'Imported! And have some errors!','errors' => 
        $import->failures()],404);                  
    }
    return response()->json(['response' => 'File send to queue! Worker will import it.'],200);

}

解决方法

您应该将控制器代码移动到可能会出现故障的队列/作业中,然后在使用时可以通过Pusher进行访问。如果您未在导入中实现shouldQueue,则可能会失败。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...