【问题标题】:How to send parameters to queues?如何将参数发送到队列?
【发布时间】:2015-12-27 17:17:08
【问题描述】:

请考虑以下工作:

<?php

namespace App\Jobs;

use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ImportUsers extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    public function __construct($number)
    {
        $this->number=$number;
    }

    public function handle()
    {
        dd($this->number);
        return;
    }
}

使用sync 队列$this-&gt;dispatch(new \App\Jobs\ImportUsers(5)); 调度此作业会抛出此异常:Undefined property: App\Jobs\ImportUsers::$number。这对我来说真的很奇怪。为什么handle方法不能访问类属性?

【问题讨论】:

    标签: php laravel queue laravel-5.1


    【解决方案1】:

    正确申报您的财产

    class ImportUsers extends Job implements SelfHandling, ShouldQueue
    {
        use InteractsWithQueue, SerializesModels;
    
        protected $number; // <-- Here
    
        public function __construct($number)
        {
            $this->number=$number;
        }
    
        public function handle()
        {
            dd($this->number);
            return;
        }
    }
    

    在将作业从队列中反序列化之后会发生什么,您会丢失动态创建的属性。

    试试看:

    $ php工匠修补匠 >>> Bus::dispatch(new App\Jobs\ImportUsers(7)); 7 >>>

    【讨论】:

    • 非常感谢。你拯救了我的一天。
    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多