Languages

Version

Theme

通知

数据库通知

数据库通知

设置通知的数据库表格

在开始之前,请确保 Laravel 通知表 已添加到你的数据库中:

# Laravel 11 and higher
php artisan make:notifications-table

# Laravel 10
php artisan notifications:table

如果你使用的是 PostgreSQL,请确保迁移中的 data 列字段使用 json(): $table->json('data')

如果你的 User 模型使用了 UUID,请确保你的 notifiable 列使用 uuidMorphs(): $table->uuidMorphs('notifiable')

在面板中启用数据库通知

如果你想在面板中接收数据库通知,你需要在配置中启用它:

use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->databaseNotifications();
}

发送数据库通知

发送数据库通知的方法有多种,请根据实际情况选择。

可以使用 fluent API:

use Filament\Notifications\Notification;

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

Notification::make()
    ->title('Saved successfully')
    ->sendToDatabase($recipient);

或者使用 notify() 方法:

use Filament\Notifications\Notification;

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

$recipient->notify(
    Notification::make()
        ->title('Saved successfully')
        ->toDatabase(),
);

Laravel 使用队列发送数据库通知。请确保你的队列正在运行以便接收通知。

或者,你也可以使用传统的 Laravel 通知类,通过 toDatabase() 方法返回通知:

use App\Models\User;
use Filament\Notifications\Notification;

public function toDatabase(User $notifiable): array
{
    return Notification::make()
        ->title('Saved successfully')
        ->getDatabaseMessage();
}

接收数据库通知

如果不进行任何设置,新的数据库通知只有在页面首次加载时才会收到。

轮询新的数据库通知

轮询是指定期向服务器发出请求以检查是否有新通知。这是一种不错的方法,因为设置简单,但有些人可能会认为它不是一种可扩展的解决方案,因为它会增加服务器负载。

默认情况下,Livewire 每 30 秒轮询一次新通知:

use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->databaseNotifications()
        ->databaseNotificationsPolling('30s');
}

你可以根据需要完全禁用轮询:

use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->databaseNotifications()
        ->databaseNotificationsPolling(null);
}

使用 Echo 通过 websocket 接收新的数据库通知

Websocket 是一种更高效的实时接收新通知的方式。要设置 Websocket,你必须先在面板中配置它

设置好 Websocket 后,你可以在发送通知时将 isEventDispatched 参数设置为 true,从而自动派发 DatabaseNotificationsSent 事件。这将触发立即为用户获取新通知:

use Filament\Notifications\Notification;

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

Notification::make()
    ->title('Saved successfully')
    ->sendToDatabase($recipient, isEventDispatched: true);

将数据库通知标记为已读

模态框顶部有一个按钮,可以一次性将所有通知标记为已读。你也可以向通知添加操作(Action),使之将单个通知标记为已读。为此,请在操作中使用 markAsRead() 方法:

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(),
    ])
    ->send();

此外,你也可以使用 markAsUnread()方法将通知标记为未读:

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(),
    ])
    ->send();

打开数据库通知模态框

你可以通过在任何地方发送 open-modal 浏览器事件来打开数据库通知模态框:

<button
    x-data="{}"
    x-on:click="$dispatch('open-modal', { id: 'database-notifications' })"
    type="button"
>
    Notifications
</button>
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

Previous
概述