如何在maatwebsite excel 3.1中跳过空白行,以便在Laravel上进行模型方式导入
我正在使用如何在maatwebsite excel 3.1中跳过空白行,以便在Laravel上进行模型方式导入,laravel,maatwebsite-excel,Laravel,Maatwebsite Excel,我正在使用maatwebsite exvel 3.1处理laravel项目,以从文件上传方法导入excel文件。这是我的StudentsImport课程 public function model(array $row) { return new Student([ 'school_uuid' => Auth::user()->school_uuid, 'cardid' => $row[0], 'prefix'
maatwebsite exvel 3.1
处理laravel项目,以从文件上传方法导入excel文件。这是我的StudentsImport
课程
public function model(array $row)
{
return new Student([
'school_uuid' => Auth::user()->school_uuid,
'cardid' => $row[0],
'prefix' => $row[1],
'name' => $row[2],
'lastname' => $row[3],
'dob' => $row[4],
'address' => $row[5],
'phone' => $row[6],
]);
}
下面是控制器
Excel::import(new StudentsImport, $request->file('file'));
代码工作正常。我可以将excel的数据导入数据库,但也可以导入空行。在放入数据库之前,我想过滤/验证以跳过这些空白。非常感谢您就此提供任何建议或指导。根据提供的,支持使用Laravel的验证来防止插入无效行
要使用它,请在导入器类上实现with validation
接口,并添加一个rules()
方法,该方法返回应用于确保行有效的验证规则
公共函数规则():数组
{
返回[
“0”=>“必需|字符串”,
“1”=>“必需|字符串”,
“2”=>“必需|数字”,
//诸如此类
];
}
<> >而不是直接将文件直接传递给这个函数,而是创建一个函数来过滤具有空白字段的数据,如下面的内容:
使用如下函数:
foreach ($import->failures() as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
$failure->values(); // The values of the row that has failed.
}
请注意,
rules()
然而,我发现文档中存在此功能,只需将其实现到导入类:“Maatwebsite\Excel\Concerns\SkipsOnFailure”
例如:
class UsersImport implements ToModel, WithValidation, SkipsOnFailure {
// Your import class
}
然后在导入类中定义一个方法来处理错误:
/**
* @param Failure[] $failures
*/
public function onFailure(Failure ...$failures)
{
// Handle the failures how you'd like.
}
现在rules()
方法将不再在空行上抛出错误,您可以静默地处理错误,您应该仍然使用rules()
方法tho,因为您需要它来抛出现在可以由您处理的错误,也许您可以将它们记录到文件中,或者以正常方式执行
最后,您将能够收集所有错误,以下是文档中的一个示例:
$import = new UsersImport();
$import->import('users.xlsx');
dd($import->errors());
因此,现在您可以静默地捕获错误并最终将其返回到您的请求中,您甚至可以返回如下详细信息:
foreach ($import->failures() as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
$failure->values(); // The values of the row that has failed.
}
这不是复制粘贴材料好的,我只是解释了如何根据文档中给出的示例访问错误,我让您来处理这些数据
这里是我找到所有这些信息的文档的链接:您也可以根据所做的验证跳过行。如果您的一个请求没有覆盖,您可以在模型中输入一个返回null
公共函数模型(数组$row)
{
如果($row[2]==null | |$row[3]==null…){
返回null;
}
归还新生([
'school\u uuid'=>Auth::user()->school\u uuid,
“cardd”=>$row[0],
'前缀'=>$row[1],
'name'=>$row[2],
'lastname'=>$row[3],
'dob'=>$row[4],
“地址”=>$row[5],
“电话”=>$row[6],
]);
}
尝试使用ToCollection,然后在($row->filter()->isNotEmpty())中尝试
不要忘记包含使用Maatwebsite\Excel\Concerns\ToCollection代码>
这对我来说非常有效
可以找到引用。如果数组包含空字符串的值,则这将不起作用。
foreach ($import->failures() as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
$failure->values(); // The values of the row that has failed.
}
public function collection(Collection $rows)
{
foreach($rows as $row) {
if($row->filter()->isNotEmpty()){
// you logic can go here
}
}
}