【发布时间】:2017-09-24 13:24:01
【问题描述】:
恕我直言,当前在 Laravel 中保存通知的数据库通道设计非常糟糕:
- 例如,您不能在项目上使用外键级联来清理已删除项目的通知
- 在
data列(强制转换为数组)中搜索自定义属性不是最佳选择
您将如何扩展供应商包中的 DatabaseNotification 模型?
我想将列 event_id、question_id、user_id(创建通知的用户)等添加到默认 laravel notifications 表中
如何覆盖send 函数以包含更多列?
在:
vendor/laravel/framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php
代码:
class DatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
'type' => get_class($notification),
\\I want to add these
'user_id' => \Auth::user()->id,
'event_id' => $notification->type =='event' ? $notification->id : null,
'question_id' => $notification->type =='question' ? $notification->id : null,
\\End adding new columns
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
]);
}
}
【问题讨论】:
标签: laravel laravel-5 laravel-6