Skip to content

Add the database notification channel - #396

Open
turgutahmet wants to merge 1 commit into
laravel:0.xfrom
turgutahmet:database-notifications
Open

turgutahmet wants to merge 1 commit into
laravel:0.xfrom
turgutahmet:database-notifications

Conversation

@turgutahmet

Copy link
Copy Markdown

Problem

#178 left the database notification channel for later, so there's no built-in way to keep notifications around for an in-app list (the usual bell icon with unread notifications).

Change

  • #[notification] accepts a database method that returns any Serialize value. It's stored with the notification's type, the struct name in snake case (invoice_paid):
    #[notification]
    impl InvoicePaid {
        fn database(&self, _user: &User) -> InvoicePaidData {
            InvoicePaidData { invoice_id: self.invoice.id, amount: self.invoice.total }
        }
    }
  • The Database channel is built with #[notification::channel], like Slack, so queueing, channels, transactions, and the fake work as they do for other channels.
  • Any model can receive it. Rows are keyed by the model's morph name and key, and Uuid keys bind natively, so uuid_morphs works on PostgreSQL.
  • Reading back:
    DatabaseNotification::of(&user).unread().get().await?;
    DatabaseNotification::of(&user).unread().count().await?;
    DatabaseNotification::of(&user).find(id).await?; // only the user's own notifications
    DatabaseNotification::of(&user).unread().mark_as_read().await?;
    notification.mark_as_read().await?;
  • laravel::notification::table!() creates the table, using the same schema as Laravel 13's notifications stub.
  • Each message gets a UUIDv7 when it's built and the insert is insert_or_ignore, so a retried queued delivery doesn't store it twice.
  • Docs: a "Database notifications" section in notifications.md, and the new macro in migrations.md.

Tests

tests/notifications_database.rs, with the main contracts also run as ignored tests on PostgreSQL and MySQL. I ran them locally against PostgreSQL 18 and MySQL 8.4.

Not included: the broadcast channel, a custom stored type (like databaseType()), a configurable table, and pruning.

A `database` method in a `#[notification]` impl stores the notification in
the `notifications` table for every model it's sent to. Models read their
notifications back with `DatabaseNotification::of`, and mark them as read
or unread one at a time or all at once. `laravel::notification::table!()`
creates the table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant