【问题标题】:How to add field to notification laravel如何向通知laravel添加字段
【发布时间】:2020-03-03 14:24:53
【问题描述】:

我正在使用 laravel 并使用通知系统

所以我在通知表中添加一列“URL”并希望将其存储在数据库中,但它不起作用,我真的不知道如何添加字段并存储在数据库中

这是我的通知类,并在提交新评论时通知:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class NewComment extends Notification
{
    use Queueable;
    private $details;


    /**
     * Create a new notification instance.
     *
     * @return void
     */
     public function __construct($details)
     {
       $this->details = $details;

     }


    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
      return ['database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
      //
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
      return [
       'message' => $this->details['message'],
       'product' => $this->details['product'],
       'url' => 'test'
   ];

    }
}

这里是控制器

  $details = [
          'message' => 'یک نظر جدید برای محصول',
          'product' => $product->title
      ];
    $user->notify(new NewComment($details));

它给了我这个错误:

一般错误:1364 字段 'url' 没有默认值

【问题讨论】:

    标签: php laravel notifications


    【解决方案1】:

    toArray(或toDatabase)方法返回的结果以 JSON 格式存储在名为 data 的现有列中;因此,您不需要(最好不要)向现有表中添加新列。

    只需删除该列,它应该可以工作。稍后您可以通过从 data 列解码 JSON 来读取 url

    $data = json_decode(DB::table('notifications')->first()->data);
    $url = $data->url;
    

    【讨论】:

    • 在寻找如何在表格中添加另一个字段时,我遇到了您的回复....直到我阅读了您的回复(当然!!)我才意识到我可以只需通过 DATA 字段发送我的附加数据(喘气!)。谢谢!
    • @AndrewFox 不客气:D
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多