我正在将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
方法和子作业类,这些子作业类具有从父类的handle
方法调用的run
方法,我想在子作业类的run
方法中能够使用类型提示来引用存储库,但是这样会导致父类中出现“参数过少”的错误。 - Roberthandle(UserRepository $repository, FooService $fooService)
并通过向父级传递FooService
来开始:parent::__handle($fooService)
。 - Jason