laravel 5.3数据库通知定制

时间:2017-04-06 14:40:20

标签: php laravel-5

我正在创建laravel 5.3数据库通知。我已根据https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/10上发布的视频创建了通知, 现在我想根据我的要求将自定义字段添加到通知表中。 请帮助我如何将自定义数据传递给通知并访问它。

1 个答案:

答案 0 :(得分:0)

当我需要将自定义字段放入Notification时,我只需要放在data字段(因为它是Json字段)就可以完美地工作。像这样:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class TaskNotification extends Notification
{
    use Queueable;

    private $message;

    /**
     * @param String $message
     */
    public function __construct($message=false)
    {
        if ($message)
            $this->message = $message;
    }

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

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'message' => $this->message,
            'link' => route('mymodel.show'),
            'task'=> 1, // This is one variable which I've created
            'done'=> 0 // This is one variable which I've created
        ];
    }
}