Reference

Storage and persistence

Understand the default Eloquent store and the preference table schema.

The default store is EloquentNotificationPreferenceStore. It requires a persisted Eloquent model as the notifiable and identifies it with its morph class and primary key.

The migration creates these fields:

  • notifiable_type and notifiable_id identify the recipient.
  • notification_key and channel identify the preference.
  • context_key stores an empty string for global preferences or a context key such as team:12.
  • enabled stores the explicit boolean value.

The unique index prevents duplicate values for the same recipient, notification, channel, and context. Bind MutableNotificationPreferenceStore to another implementation when your application uses a different persistence layer.

The Eloquent store rejects unsaved models because their identity is not stable enough for preference persistence.

Context preference store

EloquentNotificationContextPreferenceStore stores policies that belong to a context rather than a notifiable. Its table contains context_key, notification_key, channel, enabled, and mode, with a unique index across the context, notification, and channel fields. Use NotificationContextPreferenceManager for application-level policy operations; the store remains the persistence abstraction behind that manager.

app/Actions/SetContextNotificationPolicy.php
use NotificationCompass\Managers\NotificationContextPreferenceManager;
use NotificationCompass\ValueObjects\NotificationContext;

$administrator = auth()->user();

app(NotificationContextPreferenceManager::class)->disable(
    $administrator,
    new NotificationContext('team', $teamId),
    'incident.created',
    'mail',
);

The default table is notificationcompass_context_preferences. Change notificationcompass.context_table before migrating when the application uses another table name. Bind MutableNotificationContextPreferenceStore to replace the Eloquent implementation.

Resolved preference cache

The resolved preference cache is enabled by default. Configure it when preference decisions are evaluated frequently during notification delivery:

config/notificationcompass.php
'cache' => [
    'enabled' => true,
    'store' => null,
    'ttl' => 300,
    'prefix' => 'notificationcompass:preferences',
],

The cache is backend-agnostic and uses the Laravel cache repository selected by cache.store. When cache.store is null, Laravel's cache.default store is used. Preference writes invalidate the relevant notifiable or context version, so stale resolved decisions are not reused after a policy change.