当我尝试使用Laravel notification时,以SmsChannel身份为Laravel通知创建了一个简单的驱动程序后,我得到了这个错误:
TypeError
App\Channels\SmsChannel::send(): Argument #2 ($notification)
must be of type Illuminate\Notifications\Notification, Modules\User\Notifications\SendVerifyCode given,
called in /var/www/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php on line 148 SmsChannel类:
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class SmsChannel
{
public function send($notifiable, Notification $notification)
{
return $notification->toSms($notifiable);
}
}和SendVerifyCode通知类:
<?php
namespace Modules\User\Notifications;
use App\Channels\SmsChannel;
use App\Models\ActivationCode;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SendVerifyCode implements ShouldQueue
{
use Queueable, InteractsWithQueue;
private string $mobile_number;
public function __construct(string $mobile_number)
{
$this->mobile_number = $mobile_number;
}
public function via(): array
{
return [SmsChannel::class];
}
public function toSms()
{
$code = ActivationCode::createCode($this->mobile_number);
}
public function toArray(): array
{
return [
'mobile_number' => $this->mobile_number
];
}
}composer-dumpautoload命令无法解决此问题
发布于 2021-05-17 16:25:43
在创建自定义通知时,您应该扩展Laravel的基本通知类。
<?php
namespace Modules\User\Notifications;
use App\Channels\SmsChannel;
use App\Models\ActivationCode;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\InteractsWithQueue;
class SendVerifyCode extends Notification implements ShouldQueue
{https://stackoverflow.com/questions/67566185
复制相似问题