Open simonhamp opened 5 years ago
In my project I added validation for each row & it has been super valuable Appending the errors to an error column allows very easy debugging In my project, outputs an invalid.csv which makes it easy to re-import only error rows after fixing
public function checkValidation($row)
{
$rules = $this->importClass->getRules();
$validator = Validator::make($row, $rules);
$is_invalid = $validator->fails();
if ($is_invalid) {
$row['errors'] = implode(' ', $validator->messages()->all());
$this->invalid[] = $row;
}
return $is_invalid;
}
I also had a duplicate check that does the same but duplicates.csv
public function checkDuplicates($row)
{
if (!isset($this->importClass->duplicate) || 0 == count($this->importClass->duplicate)) {
return false;
}
$conditions = collect($row)->intersectByKeys(array_flip($this->importClass->duplicate))->toArray();
$models = app($this->importClass->model)
->withoutGlobalScopes()
->withoutTrashed()
->where($conditions)
->get();
$is_duplicate = 0 != $models->count();
if ($is_duplicate) {
$resource = strtolower(str_plural(class_basename($this->importClass->model)));
$row['links'] = $models->map(function ($model) use ($resource) {
return url("admin/resources/$resource/$model->id");
})->implode(', ');
$this->duplicate[] = $row;
}
return $is_duplicate;
}
Like when a field is required but not yet matched up to a column