Log all notifications sent by your Laravel app
We've released a new package called Laravel Notification Log. True to its name, it will log when your Laravel app sends notifications. It also contains helpful methods to determine when notifications were sent.
Using the package
By default, the package will write an entry in the notification_log_items
table for each notification sent in your app. You can also have fine-grained control over which notifications should be logged.
The notification_log_items
table has these columns:
-
notification_type
: the class name of the sent notification -
notifiable_id
: the key value of the notifiable the notification was sent to -
notifiable_type
: the (morph) class name of the notifiable the notification was sent to -
channel
: the name of the channel the notification was sent to -
fingerprint
: a value that you can customize to identify the exact content of the notification -
extra
: an array of values you can freely add to the log -
anonymous_notifiable_properties
: the configuration of the notifiable when sending an on-demand notification -
confirmed_at
: will contain the time theNotificationSent
event was fired
How the package fills all these properties can be fully customized.
You can use the Spatie\NotificationLog\Models\NotificationLogItem
model to query logged notifications.
use Spatie\NotificationLog\Models\NotificationLogItem;
// returns all logged notifications
NotificationLogItem::query()->orderByDesc('id')->get();
The model has a handy latestFor
method that will return the single latest logged notification for a given notifiable.
/*
* Will return the single most recent sent log item for the given notifiable.
* If there was no notification sent yet to the notifiable, `null` will be returned.
*/
$logItem = NotificationLogItem::latestFor($notifiable);
The latestFor
has a couple of optional parameters to search for the latest log item corresponding to the given restrictions.
use Spatie\NotificationLog\Models\NotificationLogItem;
use App\Notifications\OrderSentNotification;
$logItem = NotificationLogItem::latestFor(
$notifiable,
notificationType: OrderSentNotification::class, // search for a specific notification type
before: $carbon, // we're looking for a notification before the given carbon instance
after: $carbon, // we're looking for a notification after the given carbon instance
fingerprint: 'dummy-fingerprint' // search for a log item with this fingerprint
);
The notificationType
parameter can be an array, in which case latestFor
will return a log item with the notification type that was most recently sent.
Getting the notifications for a user
You can add the HasNotifiableHistory
trait on a notifiable. This trait contains a few methods to work the notification history.
You should apply it to a notifiable, such as a User
model.
use Spatie\NotificationLog\Models\Concerns\HasNotifiableHistory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasNotifiableHistory;
}
Here's how to use the log to render a view with all sent notifications for a user.
<ul>
@foreach($user->loggedNotifications() as $sentNotification)
<li>{{ $sentNotification->type }} at {{ $sentNotification->created_at->format('Y-m-d H:i:s') }}</li>
@endforeach
</ul>
Using history on a notification
The package offers a HasHistory
trait that contains a couple of functions that allow you to determine if a similar notification was recently sent.
To get started, first add the trait to your notification.
namespace App\Notifications;
use Illuminate\Notifications\Notificationuse Spatie\NotificationLog\Models\Concerns\HasHistory;
class YourNotification extends Notification
{
use HasHistory;
}
Imagine that your notification should only be sent if a similar notification wasn't recently sent.
public function shouldSend($notifiable)
{
return $this
->wasNotSentTo($notifiable)
->inThePastMinutes(30);
}
In closing
The Laravel Notification Log package contains will log all notifications by your app, and offers handy methods to query to log. The package has more options. Learn them in our extensive docs.
This is one of many packages our team has made. Please look at this extensive list of Laravel and PHP packages we've made before. I'm sure there's something there for your next project. If you want to support our open-source efforts, consider picking up one of our paid products or subscribe at Mailcoach and/or Flare.
What are your thoughts on "Log all notifications sent by your Laravel app"?