在laravel中创建自定义通知通道时获取变量

在laravel中创建自定义通知通道时获取变量,laravel,laravel-5,notifications,sms,Laravel,Laravel 5,Notifications,Sms,在Laravel文档中,有一节解释了如何创建自定义通知类,URL为: 所以我创建了一个名为“MessageResponsed”的通知类,我想为它定义一个自定义SMS频道 在MessageResponsed类中,代码如下所示: <?php namespace App\Notifications; use App\Channels\SmsChannel; use App\WorkCase; use Illuminate\Bus\Queueable; use Illuminate\Noti

在Laravel文档中,有一节解释了如何创建自定义通知类,URL为:

所以我创建了一个名为“MessageResponsed”的通知类,我想为它定义一个自定义SMS频道

在MessageResponsed类中,代码如下所示:

<?php

namespace App\Notifications;

use App\Channels\SmsChannel;
use App\WorkCase;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class MessageReplied extends Notification
{
use Queueable;
public $workCase;

/**
 * Create a new notification instance.
 *
 * @param WorkCase $workCase
 */
public function __construct(WorkCase $workCase)
{
    $this->workCase = $workCase;
}

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

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->markdown('mail.message_replied', ['workCase' => $this->workCase])
                ->subject('new Message');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        'workCase' => $this->workCase
    ];
}

/**
 * @return array
 */
public function toSms()
{
    return [
        'foo' => 'bar'
    ];
}


}
来自:

$message=$notification->toSms()

在SmsChannel类的send方法中:

public function send($notifiable, Notification $notification)
{
    $message = $notification->toSms(notifiable);
    $bar = $message['bar'];
}
$message = $notification->toSms();
public function send($notifiable, Notification $notification)
{
    $message = $notification->toSms(notifiable);
    $bar = $message['bar'];
}