如何在 laravel 通知中使用 via 参数调用自定义函数?
How to call custom made function usin via parameter in laravel notification?
devs,我正在使用 laravel 8 我在里面创建了一个通知,我在下面的代码中创建了自己的 toTwilio 函数。
问题:如何调用该函数。我在 via() 函数的 return 参数中包含
但它显示“不支持驱动程序 [twilio]”。我没有注册任何东西
任何地方。我试图更改仍然显示错误的函数名称
“不支持驱动程序[]。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class DepartmentNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['toTwilio'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
public function toTwilio($notifiable)
{
echo "twilio hit";
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
您可以在此处找到更多详细信息 https://laravel.com/docs/8.x/notifications#sending-notifications。
但基本上,您可以通过以下两种方式发送通知:
1) 使用 notifiable
特征
use Notifiable;
$user->notify(new DepartmentNotification());
2) 使用通知门面
use Illuminate\Support\Facades\Notification;
Notification::send($users, new DepartmentNotification());
此外,您似乎正在尝试对通知进行排队。这意味着您可能还需要 运行、php artisan queue:work
。
在此处阅读有关自定义通知渠道的官方文档:https://laravel.com/docs/8.x/notifications#custom-channels。
首先你应该创建一个 TwilioChannel
class 来调用 toTwilio
方法:
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class TwiolioChannel
{
public function send($notifiable, Notification $notification)
{
$message = $notification->toTwilio($notifiable);
// Send notification to the $notifiable instance...
}
}
创建频道后 class 更改通知 via
方法如下:
public function via($notifiable)
{
return [TwilioChannel::class];
}
devs,我正在使用 laravel 8 我在里面创建了一个通知,我在下面的代码中创建了自己的 toTwilio 函数。
问题:如何调用该函数。我在 via() 函数的 return 参数中包含
但它显示“不支持驱动程序 [twilio]”。我没有注册任何东西
任何地方。我试图更改仍然显示错误的函数名称
“不支持驱动程序[
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class DepartmentNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['toTwilio'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
public function toTwilio($notifiable)
{
echo "twilio hit";
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
您可以在此处找到更多详细信息 https://laravel.com/docs/8.x/notifications#sending-notifications。
但基本上,您可以通过以下两种方式发送通知:
1) 使用 notifiable
特征
use Notifiable;
$user->notify(new DepartmentNotification());
2) 使用通知门面
use Illuminate\Support\Facades\Notification;
Notification::send($users, new DepartmentNotification());
此外,您似乎正在尝试对通知进行排队。这意味着您可能还需要 运行、php artisan queue:work
。
在此处阅读有关自定义通知渠道的官方文档:https://laravel.com/docs/8.x/notifications#custom-channels。
首先你应该创建一个 TwilioChannel
class 来调用 toTwilio
方法:
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class TwiolioChannel
{
public function send($notifiable, Notification $notification)
{
$message = $notification->toTwilio($notifiable);
// Send notification to the $notifiable instance...
}
}
创建频道后 class 更改通知 via
方法如下:
public function via($notifiable)
{
return [TwilioChannel::class];
}