For each notification sent in your app, a row in the notification_log_items
table will be created. You can use the Spatie\NotificationLog\Models\NotificationLogItem
model to query logged notifications.
use Spatie\NotificationLog\Models\NotificationLogItem;
NotificationLogItem::query()->orderByDesc('id')->get();
##Getting the latest log item for a notifiable
The model has a handy latestFor
method that will return the single latest logged notification for a given notifiable.
$logItem = NotificationLogItem::latestFor($notifiable);
The latestFor
has a couple of optional parameters to search for the latest log item that corresponds to the given restrictions.
use Spatie\NotificationLog\Models\NotificationLogItem;
use App\Notifications\OrderSentNotification;
$logItem = NotificationLogItem::latestFor(
$notifiable,
notificationType: OrderSentNotification::class,
before: $carbon,
after: $carbon,
fingerprint: 'dummy-fingerprint'
);
The notificationType
parameter can be an array, in which case latestFor
will return a log item with notification type that was most recently sent.
##Using the notifiable to search logged notifications
The package provides a trait Spatie\NotificationLog\Models\Concerns\HasNotifiableHistory
that you can use on a notifiable.
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Notifiable;
use HasNotifiableHistory;
}
This trait will add a method notificationLogItems
that will return a MorphMany
relation to all notifications that were logged for the notifiable.
$loggedNotifications = $user->notificationLogItems;
$loggedNotifications = $user->notificationLogItems()->limit(5)->get();
Additionally, the trait provides a latestLoggedNotification
method that will return the latest logged notification for the notifiable.
$logItem = $user->latestLoggedNotification();
The method has a couple of optional parameters to search for the latest log item that corresponds to the given restrictions.
use App\Notifications\OrderSentNotification;
$logItem = $user->latestLoggedNotification(
notificationType: OrderSentNotification::class,
before: $carbon,
after: $carbon,
fingerprint: 'dummy-fingerprint'
);
The notificationType
parameter can be an array, in which case latestFor
will return a log item with notification type that was most recently sent.
##Determining sent notifications within 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\Notification;
use 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);
}
By default, wasNotSentTo
will not take into account the fingerprint of a logged notification. To take it into account, set the named argument withSameFingerprint
to true.
public function shouldSend($notifiable)
{
$this
->wasNotSentTo($notifiable, withSameFingerprint: true)
->inThePastMinutes(30);
}
In addition to wasNotSentTo
, the trait also has a method wasSentTo
.