如何解决Laravel - Excel Maatwebsite 导入失败时未显示失败错误
使用 Laravel-5.8 和 Maatwebsite-3.1,我有一个名为员工(员工)的表。
Excel 导入:
class EmployeeGradeLevelsImport implements
ToCollection,WithHeadingRow,SkipsOnError,WithValidation,SkipsOnFailure
{
use Importable,SkipsErrors,SkipsFailures;
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
$this->gradelevel = $row['grade_level_code'];
HrEmployee::where('company_id',auth()->user()->company_id)
->where('employee_code',$row['staff_id'])
->where('email',$row['official_email'])
->update([
'grade_level_id' => $this->getGradeLevel(),'employee_code' => $row['staff_id'],'email' => $row['official_email'],]);
}
}
public function rules(): array
{
return [
'*.employee_code' => [
'required','min:2','max:20',],'*first_name' => [
'required','string','min:3','max:50'
],'*last_name' => [
'required','*grade_level_code' => 'required','*official_email' => [
'required','email',// official email
'max:100',];
}
public function customValidationAttributes()
{
return [
'company_id' => 'Company','grade_level_code' => 'Grade Level Code','employee_code' => 'Staff ID','first_name' => 'First Name','last_name' => 'Last Name',];
}
public function getGradeLevel(){
if(!empty($this->gradelevel) || !$this->gradelevel){
return HrGradeLevel::where('grade_level_code',$this->gradelevel)->where('company_id',Auth::user()->company_id)->pluck('id')->first();
} else {
return 0;
}
}
public function headingRow(): int
{
return 1;
}
}
这是我的控制器功能
控制器:
public function importEmployeeGradeLevel(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 EmployeeGradeLevelsImport;
$import->import($pathTofile);
if($import->failures()->isNotEmpty()){
$failures = $import->failures();
Session::flash('failures',$failures);
return view('hr.employees.import_employee_grade_form')->with('failures',$failures);
}
Session::flash('success','Employee Grade Records Update Imported Successfully');
return redirect()->route('hr.employees.index');
}
}
最后查看刀片错误信息:
@if (Session::has('failures'))
<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
我想使用 excel 上传来更新以下两 (2) 个字段:电子邮件和 Grade_level_id 为每个员工,其中:
company_id = Auth::user()->company_id AND employee_code = $row['staff_id']。
此外,它应该只对满足条件的人执行更新。我应该只执行更新而不是创建。
我观察到,当出现故障时,它不会更新失败的消息,但只会排除失败的消息。它也会显示成功,但有些不符合条件(虽然它不会更新那些。)
在控制器中,我 dd($import);
但是得到了这个:
App\Imports\EmployeeGradeLevelsImport {#1140 ▼
#output: null
#errors: []
#failures: []
+"gradelevel": null
}
是的,有些失败了。
我该如何纠正?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。