我正在从我的控制器向我的队列中添加一个LARAVEL作业(job)

$this->dispatchFromArray(
    'ExportCustomersSearchJob',
    [
        'userId' => $id,
        'clientId' => $clientId
    ]
);

我想在实现ExportCustomersSearchJob类时将userRepository作为依赖项注入.请问我怎么才能做到这一点呢?

I have this but it doesn't work

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;
    }
}

推荐答案

You inject your dependencies in the handle method:

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相关问答推荐

当使用Craftable PRO时,Wysiwig是什么时候生成的?

使用 Laravel 10 在 Blade 中添加成功消息

Dompdf 古吉拉特语和印地语文本未正确显示

Laravel 语法 GroupBy 获取列到数组

如何在 Laravel 中填充以后执行的条件

为什么 laravel 模型会重复一组数据以及如何(如果可能)只有一组数据?

克隆后设置 Laravel 项目

从 laravel/blade 中的数组创建逗号分隔列表?

Laravel - 验证 - 如果字段为空则需要

如何使用 ajax 请求删除 laravel 5.3 中的记录?

在 Laravel Blade 中转义 vue.js 数据绑定语法?

找不到列:1054字段列表中的未知列0-Laravel-我的代码中的任何地方都没有0列

在 Laravel 中下载后如何重定向?

SQLSTATE [42000]:语法错误或访问冲突:1066 Not unique table/alias on relationship

使用数据库用户提供程序时如何在 Laravel 中创建密码重置方法

如何处理 laravel 5 中的私有图像?

约定表名(带下划线)

Laravel 4 上传图片表单

WhereHas Laravel 中的关系计数条件是什么

Laravel 存储链接不适用于生产