将参数传递给Laravel作业不起作用 [英] Passing parameters to Laravel job is not working

查看:56
本文介绍了将参数传递给Laravel作业不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前已经阅读过该问题的解决方案,但似乎我在做其他事情,这就是为什么我要问:通常,解决方案是将参数以及__construct方法添加到类的主体中但即使这样做也行不通.

I've read solutions to this problem before but it seems that I'm doing something else wrong and that's why I'm asking: Usually the solution is adding the parameters to the body of the class as well as the __construct method but even doing that it doesn't work.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
use App\Mail\TasksFinished;

class SendMailFinished implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public $tries = 3;

    protected $msg;

    protected $subj;

    protected $mailto;

    public function __construct($msg, $subj, $mailto)
    {
        //
        $this->msg = $msg;
        $this->subj = $subj;
        $this->mailto = $mailto;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //Envia correo cuando termine cola.
        Mail::to($this->mailto)->queue(new TasksFinished($this->msg, $this->subj));
    }

我尝试在修补程序中以这种方式通过队列来运行它:

I try to run this through a queue this way in tinker:

use App\Jobs\SendMailFinished;
$job = new SendMailFinished('Hola', 'Prueba', 'ffuentes@example.org');
$job->dispatch();

TypeError:函数参数太少App/Jobs/SendMailFinished :: __ construct(),传入0C:/laragon/www/reportes/vendor/laravel/framework/src/Illuminate/Foundation/Bus/Dispatchable.php在第16行,正好是3个

TypeError: Too few arguments to function App/Jobs/SendMailFinished::__construct(), 0 passed in C:/laragon/www/reportes/vendor/laravel/framework/src/Illuminate/Foundation/Bus/Dispatchable.php on line 16 and exactly 3 expected

为什么即使在类中和实例化期间都指定了所有参数后,为什么在分派时仍然看不到任何参数.(我也尝试过将参数公开,但这是同一回事.)

Why even after specifying all the parameters both in the class and during instantiation it still cannot see any parameters when dispatching. (I've tried making params public as well but it's the same thing).

推荐答案

调用 dispatch()时,应在此处发送参数.

When you call dispatch() you should send the parameter there.

尝试静态调用

SendMailFinished::dispatch('Hola', 'Prueba', 'ffuentes@example.org');

这篇关于将参数传递给Laravel作业不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆