Skip to content

marko/notification-database

Database notification storage --- persist, query, and manage notification read state in the database. Provides the DatabaseNotification entity, NotificationRepositoryInterface, and a DatabaseNotificationRepository implementation. Query a user’s notifications, mark them as read, count unread notifications, and clean up old ones.

Works with the database channel from marko/notification and requires marko/database for the database connection.

Terminal window
composer require marko/notification-database

Inject the repository to fetch notifications for a notifiable entity:

use Marko\Notification\Database\Repository\NotificationRepositoryInterface;
class NotificationController
{
public function __construct(
private NotificationRepositoryInterface $notificationRepository,
) {}
public function index(
User $user,
): array {
return $this->notificationRepository->forNotifiable($user);
}
public function unreadCount(
User $user,
): int {
return $this->notificationRepository->unreadCount($user);
}
}

Mark individual notifications or all at once:

// Mark one notification as read
$this->notificationRepository->markAsRead($notificationId);
// Mark all notifications as read for a user
$this->notificationRepository->markAllAsRead($user);
$unread = $this->notificationRepository->unread($user);
foreach ($unread as $notification) {
$data = json_decode($notification->data, true);
// Process notification data
}
// Delete a single notification
$this->notificationRepository->delete($notificationId);
// Delete all notifications for a user
$this->notificationRepository->deleteAll($user);

Replace the repository via Preference to add custom query logic:

use Marko\Core\Attributes\Preference;
use Marko\Notification\Database\Repository\DatabaseNotificationRepository;
use Marko\Notification\Contracts\NotifiableInterface;
use Marko\Notification\Database\Entity\DatabaseNotification;
#[Preference(replaces: DatabaseNotificationRepository::class)]
class CustomNotificationRepository extends DatabaseNotificationRepository
{
/**
* @return array<DatabaseNotification>
*/
public function forNotifiable(
NotifiableInterface $notifiable,
): array {
// Custom query logic (e.g., pagination, filtering by type)
return parent::forNotifiable($notifiable);
}
}
MethodDescription
forNotifiable(NotifiableInterface $notifiable): arrayGet all notifications for a notifiable, most recent first. Returns array<DatabaseNotification>.
unread(NotifiableInterface $notifiable): arrayGet all unread notifications for a notifiable, most recent first. Returns array<DatabaseNotification>.
markAsRead(string $notificationId): voidMark a single notification as read.
markAllAsRead(NotifiableInterface $notifiable): voidMark all notifications as read for a notifiable.
delete(string $notificationId): voidDelete a single notification by ID.
deleteAll(NotifiableInterface $notifiable): voidDelete all notifications for a notifiable.
unreadCount(NotifiableInterface $notifiable): intCount unread notifications for a notifiable.

Entity mapped to the notifications table. Accepts a ConnectionInterface connection.

PropertyTypeDescription
$idstringUUID primary key (varchar 36).
$typestringNotification class name.
$notifiableTypestringNotifiable entity class name.
$notifiableIdstringNotifiable entity ID.
$datastringJSON-encoded notification data.
$readAt?stringTimestamp when read, or null if unread.
$createdAtstringTimestamp when the notification was created.