数据库通知


设置通知数据库表
开始前,请确保已将 Laravel 通知表 添加到数据库:
bash
php artisan make:notifications-table若使用 PostgreSQL,请确保迁移中的 `data` 列使用 `json()`:`$table->json('data')`。
若 `User` 模型使用 UUID,请确保 `notifiable` 列使用 `uuidMorphs()`:`$table->uuidMorphs('notifiable')`。
在面板中启用数据库通知
若要在面板中接收数据库通知,可在配置中启用:
php
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->databaseNotifications();
}发送数据库通知
发送数据库通知有多种方式,可按最适合你的选择。
可使用我们的流式 API:
php
use Filament\Notifications\Notification;
$recipient = auth()->user();
Notification::make()
->title('Saved successfully')
->sendToDatabase($recipient);或使用 notify() 方法:
php
use Filament\Notifications\Notification;
$recipient = auth()->user();
$recipient->notify(
Notification::make()
->title('Saved successfully')
->toDatabase(),
);Laravel 通过队列发送数据库通知。请确保队列正在运行以便接收通知。
也可使用传统的 Laravel 通知类,在 toDatabase() 方法中返回通知:
php
use App\Models\User;
use Filament\Notifications\Notification;
public function toDatabase(User $notifiable): array
{
return Notification::make()
->title('Saved successfully')
->getDatabaseMessage();
}将数据库通知触发器移到面板侧栏
默认情况下,数据库通知触发器位于顶栏。若禁用顶栏,则会添加到侧栏。
可通过在配置中向 databaseNotifications() 方法传入 position 参数,选择始终将其移到侧栏:
php
use Filament\Enums\DatabaseNotificationsPosition;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->databaseNotifications(position: DatabaseNotificationsPosition::Sidebar);
}接收数据库通知
在未做任何设置时,新的数据库通知仅在页面首次加载时接收。
轮询新的数据库通知
轮询是定期向服务器发请求以检查新通知的做法。设置简单,是不错的方案,但有人认为会增加服务器负载,不够可扩展。
默认情况下,Livewire 每 30 秒轮询一次新通知:
php
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->databaseNotifications()
->databaseNotificationsPolling('30s');
}若需要,可完全禁用轮询:
php
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->databaseNotifications()
->databaseNotificationsPolling(null);
}用 Echo 通过 WebSocket 接收新的数据库通知
WebSocket 是实时接收新通知更高效的方式。要设置 WebSocket,必须先在面板中配置。
WebSocket 就绪后,发送通知时可把 isEventDispatched 参数设为 true,以自动派发 DatabaseNotificationsSent 事件。这会立即为用户拉取新通知:
php
use Filament\Notifications\Notification;
$recipient = auth()->user();
Notification::make()
->title('Saved successfully')
->sendToDatabase($recipient, isEventDispatched: true);将数据库通知标记为已读
模态顶部有一按钮可一次将全部通知标记为已读。也可为通知添加 Actions,用于将单条通知标记为已读。为此,在操作上使用 markAsRead() 方法:
php
use Filament\Actions\Action;
use Filament\Notifications\Notification;
Notification::make()
->title('Saved successfully')
->success()
->body('Changes to the post have been saved.')
->actions([
Action::make('view')
->button()
->markAsRead(),
])
->sendToDatabase($recipient);也可使用 markAsUnread() 方法将通知标记为未读:
php
use Filament\Actions\Action;
use Filament\Notifications\Notification;
Notification::make()
->title('Saved successfully')
->success()
->body('Changes to the post have been saved.')
->actions([
Action::make('markAsUnread')
->button()
->markAsUnread(),
])
->sendToDatabase($recipient);打开数据库通知模态
可通过派发 open-modal 浏览器事件,从任意位置打开数据库通知模态:
blade
<button
x-data="{}"
x-on:click="$dispatch('open-modal', { id: 'database-notifications' })"
type="button"
>
Notifications
</button>