如何解决没有失败时,Excel 上传显示错误
我使用 Laravel-5.8 和 Maatwebsite-3.1 从 Excel 导入数据库:
控制器:
public function importGrade(Request $request){
if (empty($request->file('file')))
{
return back()->with('error','File cannot be empty!');
}
else{
request()->validate([
'file' => 'required|mimes:xls,xlsx,csv|max:2048',]);
$pathTofile = $request->file('file')->store('import');
$import = new GradeLevelsImport;
$import->import($pathTofile);
if($import->failures()->isNotEmpty()){
$failures = $import->failures();
return view('hr.grades.import_grade_form')->with('failures',$failures);
}
Session::flash('success','Records Imported Successfully');
return redirect()->route('hr.grades.index');
}
Excel 导入:
class GradeLevelsImport implements
ToModel,WithBatchInserts,WithHeadingRow,SkipsOnError,WithValidation,SkipsOnFailure
{
use Importable,SkipsErrors,SkipsFailures;
public function model(array $row)
{
return new HrGrade([
'grade_level_code' => $row['grade_code'] ?? '','grade_level_name' => $row['grade_name'] ?? '','company_id' => Auth::user()->company_id,'created_by' => Auth::user()->id,'created_at' => date("Y-m-d H:i:s"),'is_active' => 1,]);
}
}
这是 Flash 会话视图:
查看刀片
@if (count($failures) >= 1)
<div class="alert alert-danger alert-block" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Failed Upload(s): </strong>
<table class=" table table-bordered table-striped table-hover">
<thead>
<tr>
<th>
Row
</th>
<th>
Attribute
</th>
<th>
Error
</th>
<th>
Values
</th>
</tr>
</thead>
<tbody>
@foreach( $failures as $validation )
<tr>
<td>
{{ $validation->row() ?? '' }}
</td>
<td>
{{ $validation->attribute() ?? '' }}
</td>
<td>
<ul>
@foreach($validation->errors() as $e)
<li>{{$e ?? '' }}</li>
@endforeach
</ul>
</td>
<td>
<ul>
@foreach($validation->values() as $v)
<li>{{$v ?? '' }}</li>
@endforeach
</ul>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
如果有故障,一切正常。它显示失败并上传成功的。
但问题在于没有失败并且一切正常。它显示此错误:
production.ERROR:未定义变量:失败
它指向这条线
@if (count($failures) >= 1)
同一行代码在出现故障的地方还可以。
如何解决这个问题?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。