Laravel 8 通知事件
当通知被发送后,通知系统会触发 Illuminate\Notifications\Events\NotificationSent 事件,该事件实例包含被通知的实体(如用户)和通知实例本身。你可以在 EventServiceProvider 中为该事件注册监听器:
/**
* 应用程序的事件监听器映射。
*
* @var array
*/
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\LogNotification',
],
]; {提示} 在
EventServiceProvider中注册监听器之后,使用 Artisan 命令event:generate可以快速生成监听器类。
在事件监听器中,可以访问事件的 notifiable、 notification 和 channel 属性以了解通知接收者和通知本身的更多信息:
/**
* 处理事件。
*
* @param NotificationSent $event
* @return void
*/
public function handle(NotificationSent $event)
{
// $event->channel
// $event->notifiable
// $event->notification
// $event->response
}