To search for logged notifications directly through a model (e.g., a User), use the Spatie\NotificationLog\Models\Concerns\HasNotifiableHistory trait.
This adds two main capabilities:
notificationLogItems: A MorphMany relationship to all logged notifications for that model.latestLoggedNotification(): A method to retrieve the single most recent log item, supporting the same restriction parameters as NotificationLogItem::latestFor().
use Illuminate\Database\Eloquent\Model;
use Spatie\NotificationLog\Models\Concerns\HasNotifiableHistory;
use Spatie\NotificationLog\Notifications\Notifiable;
class User extends Model
{
use Notifiable;
use HasNotifiableHistory;
}
// Accessing the relationship
$loggedNotifications = $user->notificationLogItems;
$recentFive = $user->notificationLogItems()->limit(5)->get();
// Accessing the latest notification with restrictions
$logItem = $user->latestLoggedNotification(
notificationType: OrderSentNotification::class,
before: $carbon,
after: $carbon,
fingerprint: 'dummy-fingerprint'
);