Level 41
I am looking at the source code for the ImportFailed event. It doesn't contain much data. Have you try dumping $event->getException() to see anything you can use?
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have an excel import class and it is working fine. I need to send email to user when any error or failure happened.
Send the failed count, records and other information to user via email. I have code need to restructure it.
<?php
namespace App\Imports;
use App\Mail\SuperAdmin\Imports\StudentImportCompletedMail;
use App\Models\Department;
use App\Models\Gender;
use App\Models\Level;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Events\AfterImport;
use Maatwebsite\Excel\Events\ImportFailed;
class UserStudentImport implements ToCollection, WithChunkReading, ShouldQueue, WithEvents, WithHeadingRow, SkipsOnFailure
{
use Importable, RegistersEventListeners, SkipsFailures;
/**
* @param Collection $rows
*/
public function collection(Collection $rows)
{
}
public function chunkSize(): int
{
return 75;
}
public function registerEvents(): array
{
return [
ImportFailed::class => function (ImportFailed $event) {
if (isset($this->importedBy->email)) {
Mail::to($this->importedBy->email)->queue(new StudentImportFailedMail());
}
},
AfterImport::class => function (AfterImport $event) {
if (isset($this->importedBy->email)) {
Mail::to($this->importedBy->email)->queue(new StudentImportCompletedMail());
Log::info('User Imported Mail Sent Sucessfully');
}
},
];
}
}
How to get failed records information from the $event object.
Please or to participate in this conversation.