本文介绍了如何向laravel作业注入依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在这样从控制器添加laravel作业到我的队列中
I'm adding a laravel job to my queue from my controller as such
$this->dispatchFromArray(
'ExportCustomersSearchJob',
[
'userId' => $id,
'clientId' => $clientId
]
);
我想在实现 ExportCustomersSearchJob
类时注入 userRepository
作为依赖项.请问我该怎么做?
I would like to inject the userRepository
as a dependency when implementing the ExportCustomersSearchJob
class. Please how can I do that?
我有这个,但是不行
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, ShouldQueue
{
use InteractsWithQueue, SerializesModels, 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作业注入依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!