Laravel 5.4类型错误:函数参数太少。 0个通过,正好1个

时间:2018-08-07 08:57:18

标签: php laravel laravel-5.4

我正在使用Laravel 5.4,我想在发送电子邮件中实现队列。我具有注册为

的功能
public function register(CustomerRequest $request)
    {
        \Log::info("Request Cycle with Queues Begins");
        $parameter = 'This is parameter';
        dispatch(new SendWelcomeEmail($parameter));

        \Log::info("Request Cycle with Queues Ends");
    }

并且我在App\Jobs\SendWelcomeEmail内创建了一个工作。如下所示:

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

    protected $parameter;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($parameter)
    {
        $this->parameter = $parameter;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info($this->parameter);
    }
}

为什么会出现类似错误

Type error: Too few arguments to function App\Jobs\SendWelcomeEmail::handle(), 0 passed and exactly 1 expected in

1 个答案:

答案 0 :(得分:0)

$parameter不是用于handle()方法,而是用于其构造函数

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

    protected $parameter;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($parameter)
    {
        $this->parameter = $parameter;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info($this->parameter);
    }
}