在 laravel 默认通知 table 中为 "type" 列添加自定义值

add custom value for "type" column in laravel default notification table

我正在使用 laravel 的默认通知系统。 table 的类型列由完整的 class 路径填充,例如 "App\Notifications\Upvoted" 。我只是像这样自己填写数据栏:

public function toDatabase($notifiable)
 {
     return [
         "post" => $this->post,
         "user" => Auth::user()
     ];
 }

如何为 "type" 列添加自定义值。

由于我是 Laravel 的新手,将不胜感激您的帮助。

您不能这样做,因为 type 字段遵循 Laravel 框架中的变形规则。

如果您需要在 Notification Table 中保存额外的数据,您可以传入一个数组,然后在 data 字段中添加一个 JSON 字段。

比如你return:

public function toArray($notifiable)
{
    return [
        'post_id' => $this->post_id,
        'user_id' => Auth::user()->id,
    ];
}

通知数据字段中的结果将是:

{ "post_id": "2", "user_id": "1" }