首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Larave无法检测到正确的通知类

Larave无法检测到正确的通知类
EN

Stack Overflow用户
提问于 2021-05-17 16:19:49
回答 1查看 26关注 0票数 0

当我尝试使用Laravel notification时,以SmsChannel身份为Laravel通知创建了一个简单的驱动程序后,我得到了这个错误:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
<?php

namespace App\Channels;

use Illuminate\Notifications\Notification;

class SmsChannel
{
    public function send($notifiable, Notification $notification)
    {
        return $notification->toSms($notifiable);
    }
}

SendVerifyCode通知类:

代码语言:javascript
复制
<?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命令无法解决此问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-17 16:25:43

在创建自定义通知时,您应该扩展Laravel的基本通知类。

代码语言:javascript
复制
<?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
{
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67566185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档