数据库通知

设置通知的数据库表格
在开始之前,请确保 Laravel 通知表 已添加到你的数据库中:
php artisan make: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();
}
