php – 如何将依赖项注入laravel作业

前端之家收集整理的这篇文章主要介绍了php – 如何将依赖项注入laravel作业前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从我的控制器中添加一个laravel作业到我的队列中
$this->dispatchFromArray(
    'ExportCustomeRSSearchJob',[
        'userId' => $id,'clientId' => $clientId
    ]
);

我想在实现ExportCustomeRSSearchJob类时将userRepository注入依赖项.请问我该怎么做?

我有这个,但它不起作用

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

    private $userId;

    private $clientId;

    private $userRepository;


    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userId,$clientId,$userRepository)
    {
        $this->userId = $userId;
        $this->clientId = $clientId;
        $this->userRepository = $userRepository;
    }
}
您在handle方法中注入依赖项:
class ExportCustomeRSSearchJob extends Job implements SelfHandling,DispatchesJobs;

    private $userId;

    private $clientId;

    public function __construct($userId,$clientId)
    {
        $this->userId = $userId;
        $this->clientId = $clientId;
    }

    public function handle(UserRepository $repository)
    {
        // use $repository here...
    }
}

猜你在找的Laravel相关文章