阅读(3947) (7)

Laravel 8 格式化数据通知

2021-07-06 09:24:46 更新

如果要将通知存储到数据库中,您应该在通知类中定义 toDatabasetoArray 方法。该方法应该接受一个 $notifiable 实体并返回一个原生的 PHP 数组。返回的数组将会被编码成为 JSON 并存储到您的 notifications 表的 data 字段中。让我们来看一个 toArray 方法的例子:

/**
 * 获取通知的数组表现。
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        'invoice_id' => $this->invoice->id,
        'amount' => $this->invoice->amount,
    ];
}