gpt4 book ai didi

laravel - 将参数传递给 Laravel 作业不起作用

转载 作者:行者123 更新时间:2023-12-04 14:34:11 28 4
gpt4 key购买 nike

我以前读过这个问题的解决方案,但似乎我做错了其他事情,这就是我问的原因:通常解决方案是将参数添加到类的主体以及 __construct 方法中,但即使这样做它不起作用。

<?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));
}

我尝试在 tinker 中以这种方式通过队列运行它:
use App\Jobs\SendMailFinished;
$job = new SendMailFinished('Hola', 'Prueba', 'ffuentes@example.org');
$job->dispatch();

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



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

最佳答案

当您调用 dispatch()你应该在那里发送参数。

尝试静态调用它

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

关于laravel - 将参数传递给 Laravel 作业不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978163/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com