我正在使用 laravel 的默认通知系统。表的类型列由完整类路径填充,例如 "App\Notifications\Upvoted" 。我只是这样自己填数据栏:
public function toDatabase($notifiable)
{
return [
"post" => $this->post,
"user" => Auth::user()
];
}
如何为“类型”列添加自定义值。
由于我是 Laravel 的新手,将不胜感激您的帮助。
你不能这样做,因为
type
Field 遵循 Laravel 框架中的变形规则。
如果您需要在通知表中保存额外的数据,您可以传入一个数组,然后将其作为 JSON 字段添加到
data
字段中。
例如你返回:
public function toArray($notifiable)
{
return [
'post_id' => $this->post_id,
'user_id' => Auth::user()->id,
];
}
通知数据字段中的结果将是:
{ "post_id": "2", "user_id": "1" }
您可以更改广播和数据库频道的
type
值。
对于数据库通道,您必须将方法
databaseType
添加到您的通知中。看:
https://github.com/laravel/framework/blob/10.x/src/Illuminate/Notifications/Channels/DatabaseChannel.php#L35
对于广播频道,您必须将方法
broadcastType
添加到您的通知中。看:
https://github.com/laravel/framework/blob/10.x/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php#L108