#
Working with Events
Laravel Ingest dispatches events at every stage of the import lifecycle. This allows you to decouple your logic: the importer handles the data, and your listeners handle notifications, logging, or cleanup.
#
Available Events
#
Tutorial: Sending a Slack Notification
Let's implement a listener that sends a summary to Slack when an import completes.
#
1. Create the Notification
php artisan make:notification ImportCompletedNotification
// app/Notifications/ImportCompletedNotification.php
public function toSlack($notifiable)
{
$run = $this->ingestRun;
return (new SlackMessage)
->success()
->content("Import '{$run->importer_slug}' finished!")
->attachment(function ($attachment) use ($run) {
$attachment->fields([
'Total' => $run->total_rows,
'Success' => $run->successful_rows,
'Failed' => $run->failed_rows,
]);
});
}
#
2. Create the Listener
php artisan make:listener SendImportSummaryListener
// app/Listeners/SendImportSummaryListener.php
namespace App\Listeners;
use LaravelIngest\Events\IngestRunCompleted;
use App\Notifications\ImportCompletedNotification;
class SendImportSummaryListener
{
public function handle(IngestRunCompleted $event): void
{
$user = $event->ingestRun->user;
if ($user) {
$user->notify(new ImportCompletedNotification($event->ingestRun));
}
}
}
#
3. Register the Listener
In your EventServiceProvider:
use LaravelIngest\Events\IngestRunCompleted;
use App\Listeners\SendImportSummaryListener;
protected $listen = [
IngestRunCompleted::class => [
SendImportSummaryListener::class,
],
];
Now, every time an import finishes, the user who started it gets a notification!