微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel 队列执行两次

如何解决Laravel 队列执行两次

我正在使用带有队列的 Laravel 通知。这是我的代码


    <?PHP
    
    namespace App\Notifications;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;
    use Illuminate\Notifications\Notification;
    use JetBrains\PHPStorm\ArrayShape;
    
    class SendVerifyEmailOTP extends Notification implements ShouldQueue
    {
        use Queueable;
    
        protected string $name,$email,$otp,$uniq_id;
    
        /**
         * Create a new notification instance.
         *
         * @param $name
         * @param $email
         * @param $otp
         * @param $uniq_id
         */
        public function __construct($name,$uniq_id)
        {
            $this->name = $name;
            $this->email = $email;
            $this->otp = $otp;
            $this->uniq_id = $uniq_id;
        }
    
        /**
         * Get the notification's delivery channels.
         *
         * @return array
         */
        public function via(): array
        {
            return ['mail','database'];
        }
    
        /**
         * Get the mail representation of the notification.
         *
         * @return MailMessage
         */
        public function toMail(): MailMessage
        {
            return (new MailMessage)
                ->subject('Verification Email')
                ->greeting('Hello ' . $this->name)
                ->line('GrayScale is one of the fastest growing peer to peer (P2P) lending platforms in Bangladesh. It connects investors or lenders looking
                for high returns with creditworthy borrowers looking for short term
                personal loans.')
                ->line('Your One Time Password (OTP) is ' . $this->otp)
                ->action('Verify Your Email',url('/api/verify-email/' . $this->email . '/' . $this->uniq_id));
        }
    
        # Saving data to the database
        #[ArrayShape(['msg' => "string"])] public function toDatabase(): array
        {
            return [
                'msg' => 'Verification Email Sent. Check InBox'
            ];
        }
    
        /**
         * Get the array representation of the notification.
         *
         * @return array
         */
        public function toArray(): array
        {
            return [
                //
            ];
        }
    }

我是这样称呼它的...


    Notification::route('mail',[$email => $user->name])
                ->notify(new SendVerifyEmailOTP($user->name,$uniq_id));

这里的问题是作业执行了两次。它甚至在数据库的作业表中有两个条目。我在这里做错了什么?如果我想使用队列,我真的需要为每个通知单独创建作业吗?我的意思是它正在工作,但只是执行了两次。

解决方法

这是因为您的通知类有两个频道(请参阅 via 方法)。假设您使用 database 连接(检查您的 .env 以获取 QUEUE_CONNECTION 的值)进行排队,您只需要将 mail 作为您的频道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。