【问题标题】:add custom value for "type" column in laravel default notification table在 laravel 默认通知表中为“类型”列添加自定义值
【发布时间】:2018-03-30 13:38:28
【问题描述】:

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

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

如何也为“类型”列添加自定义值。

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

【问题讨论】:

  • 你不应该那样做。

标签: php laravel laravel-5.4


【解决方案1】:

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

如果您需要在通知表中保存额外的数据,您可以传入一个数组,然后将其作为 JSON 字段添加到 data 字段中。

例如你返回:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2017-08-12
    • 2017-04-23
    • 2021-10-24
    • 2015-03-09
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多