Import failures as an export #3495
dillingham
started this conversation in
Ideas
Replies: 1 comment
-
array:1 [
0 => array:4 [
"title" => "t"
"body" => "b"
"author" => "Brian"
"errors" => "The title must be at least 2 characters. The body must be at least 2 characters. The selected author is invalid."
]
] <?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Facades\Excel;
class ImportErrors extends Mailable
{
use Queueable;
use SerializesModels;
protected $failures;
public function __construct($failures)
{
$this->failures = $failures;
}
public function build(): self
{
return $this
->subject('Your import has validation errors')
->html('See attached')
->attachData($this->errorExport(), 'errors.csv');
}
public function errorExport(): string
{
$errors = $this->prepareErrors();
return Excel::raw(new ExportErrors($errors), \Maatwebsite\Excel\Excel::CSV);
}
public function prepareErrors(): array
{
$attachment = [];
foreach (collect($this->failures) as $failure) {
$key = 'row-'.$failure->row();
$errors = implode(' ', $failure->errors());
if(isset($attachment[$key])) {
$attachment[$key]['errors'] .= ' ' . $errors;
} else {
$attachment[$key] = array_merge(
$failure->values(),
['errors' => $errors]
);
}
}
return array_values($attachment);
}
}
class ExportErrors implements FromCollection, WithHeadings
{
use Exportable;
public $errors;
public function __construct($errors)
{
$this->errors = $errors;
}
public function headings(): array
{
return array_keys($this->errors[0]);
}
public function collection()
{
return collect($this->errors);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Would be amazing if all failures could be formatted into an csv for email.
Which enables you to fix the errors, remove
errors
column and re-import only the failed rowsBeta Was this translation helpful? Give feedback.
All reactions