首先,我们需要知道的是,不管是Notification 还是Mail,laravel是默认有相关的监听器的,例如:
1、Notification的监听器:
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\LogNotification',
],
];
2、邮件监听器:
protected $listen = [
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\LogSendingMessage',
],
'Illuminate\Mail\Events\MessageSent' => [
'App\Listeners\LogSentMessage',
],
];
我们需要做的就是把监听器加入到,
D:\phpStudy\WWW\xxx\app\Providers\EventServiceProvider.php
然后运行:
php artisan event:generate
然后就会生成相应的listener文件,这里,我们可以在handle里卖弄打印三个事件的属性,你会很惊喜的发现,这里面有你需要的东西。然后在这里就把记录的动作完成。
public function handle(NotificationSent $event)
{
// $event->channel
// $event->notifiable
// $event->notification
}
我的代码:
public function handle(NotificationSent $event)
{
$attributes = [
'send_type' => 2,
'customer_id' => $event->notifiable->id,
'message_type' => 2,
'message_type_value' => $event->notifiable->email,
'message_params' => json_encode($event->notification->notifiable, JSON_UNESCAPED_UNICODE) ?? '',
'status_code' => 200,
'status' => 1,
'creator' => $event->notification->notifiable['admin_id'],
'updator' => $event->notification->notifiable['admin_id'],
];
Message::create($attributes);
}