Laravel 队列执行两次

Laravel Queue Executes Twice

我正在使用 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, $email, $otp, $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, $email, $otp, $uniq_id));

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

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