spatie/laravel-notification-log
Logs all notifications sent by your Laravel app, storing them as NotificationLogItems so you can query what was sent to a user, display notification history, and make sending decisions (e.g., avoid duplicates) via helpers like wasSentTo() and inThePastMinutes().
You can install the package via composer:
composer require spatie/laravel-notification-log
You can publish and run the migrations with:
php artisan vendor:publish --tag="notification-log-migrations"
php artisan migrate
Optionally, you can publish the config file with:
php artisan vendor:publish --tag="notification-log-config"
This is the contents of the published config file:
return [
/*
* This model will be used to log all sent notifications
*/
'model' => Spatie\NotificationLog\Models\NotificationLogItem::class,
/*
* Log items older than this number of days will be automatically be removed.
*
* This feature uses Laravel's native pruning feature:
* https://laravel.com/docs/12.x/eloquent#pruning-models
*/
'prune_after_days' => 30,
/*
* If this is set to true, any notification that does not have a
* `shouldLog` method will be logged.
*/
'log_all_by_default' => config('notification-log.log_all_by_default'),
/*
* By overriding these actions, you can make low level customizations. You can replace
* these classes by a class of your own that extends the original.
*
*/
'actions' => [
'convertEventToModel' => Spatie\NotificationLog\Actions\ConvertNotificationSendingEventToLogItemAction::class
],
/*
* The event subscriber that will listen for the notification events fire by Laravel.
* In most cases, you don't need to touch this. You could replace this by
* a class of your own that extends the original.
*/
'event_subscriber' => Spatie\NotificationLog\NotificationEventSubscriber::class,
];
This package will store all sent notifications in the notification_log_items table. The related NotificationLogItems models uses the Laravel's MassPrunable trait. In the notification-log config file, you can specify the maximum age of a model in the prune_after_days key. Don't forget to schedule the model:prune command, as instructed in Laravel's docs. You'll have to explicitly add the model class:
// in routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('model:prune', [
'--model' => [
\Spatie\NotificationLog\Models\NotificationLogItem::class,
],
])->daily();
// in app/Console/Kernel.php
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule)
{
$schedule->command('model:prune', [
'--model' => [
\Spatie\NotificationLog\Models\NotificationLogItem::class,
],
])->daily();
// ...
}
}
How can I help you explore Laravel packages today?