如何解决Laravel - Maatwebsite 导入失败重定向到登录页面
在我的 Laravel-5.8 中,我使用 Maatwebsites-3.1 从 Excel 导入数据库:
public function importGrade(Request $request){
$request->validate([
'file' => 'required|max:10000|mimes:xlsx,xls',]);
$path1 = $request->file('file')->store('import');
$path=storage_path('app').'/'.$path1;
$import = new GradeLevelsImport;
$import->import($path);
if ($import->failures()->isNotEmpty()) {
Session::flash('error',$import->failures());
return redirect()->route('hr.grades.index');
}
Session::flash('success','Grade Records Imported Successfully');
return redirect()->route('hr.grades.index');
}
失败时,它会将我重定向到登录页面。问题来自这里:
if ($import->failures()->isNotEmpty()) {
Session::flash('error',$import->failures());
return redirect()->route('hr.grades.index');
}
即使我将其更改为:
if ($import->failures()->isNotEmpty()) {
Session::flash('error',$import->failures());
return back();
}
甚至当我使用:
return back()->withFailures($import->failures());
问题依然存在。
如何解决?
谢谢
解决方法
https://docs.laravel-excel.com/3.1/imports/validation.html#handling-validation-errors
您应该使用 try-catch 捕获错误。
try {
$import->import($path);
} catch(\Maatwebsite\Excel\Validators\ValidationException $e) {
Session::flash('error',$e->failures());
return redirect()->route('hr.grades.index');
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。