首页 > 解决方案 > Laravel 中的 Slack 通知错误参数太少

问题描述

我正在尝试在我的 laravel 项目中实现松弛通知。

在我的控制器内部,我调用:

$user->notify(new WorkAdded());

这里的WorkAdded

public function __construct()
{
    //
}

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


/**
 * Get the Slack representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return SlackMessage
 */
public function toSlack($notifiable)
{
    return (new SlackMessage)
                ->content('TEST');
}

User我添加的课程中:

use Notifiable;

public function routeNotificationForSlack($notification)
{
    return 'https://hooks.slack.com/services/...';
}

但是当我调用通知函数时,出现以下错误:

类型错误:函数 App\User::routeNotificationForSlack() 的参数太少,在第 30 行的 /Applications/MAMP/htdocs/gest/vendor/laravel/framework/src/Illuminate/Notifications/RoutesNotifications.php 中传递了 0,正好是 1预期的

标签: laravelslack

解决方案


如果您使用的是 Laravel 5.5 或更低版本,则routeNotificationFor{driver}方法不会传递$notification实例。

将您的方法更改为以下应该是您所需要的:

public function routeNotificationForSlack()
{
    return 'https://hooks.slack.com/services/...';
}

使用 Laravel 文档时,最好将屏幕右上角的下拉菜单更改为您正在使用的 Laravel 版本。


推荐阅读