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

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

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

我有这个,但它不起作用

  1. class ExportCustomeRSSearchJob extends Job implements SelfHandling,ShouldQueue
  2. {
  3. use InteractsWithQueue,SerializesModels,DispatchesJobs;
  4.  
  5. private $userId;
  6.  
  7. private $clientId;
  8.  
  9. private $userRepository;
  10.  
  11.  
  12. /**
  13. * Create a new job instance.
  14. *
  15. * @return void
  16. */
  17. public function __construct($userId,$clientId,$userRepository)
  18. {
  19. $this->userId = $userId;
  20. $this->clientId = $clientId;
  21. $this->userRepository = $userRepository;
  22. }
  23. }
您在handle方法中注入依赖项:
  1. class ExportCustomeRSSearchJob extends Job implements SelfHandling,DispatchesJobs;
  2.  
  3. private $userId;
  4.  
  5. private $clientId;
  6.  
  7. public function __construct($userId,$clientId)
  8. {
  9. $this->userId = $userId;
  10. $this->clientId = $clientId;
  11. }
  12.  
  13. public function handle(UserRepository $repository)
  14. {
  15. // use $repository here...
  16. }
  17. }

猜你在找的Laravel相关文章