Laravel9 Spatie QueryBuilder 在作业队列中

问题描述 投票:0回答:1

我有一个在队列中运行的作业。有一个 Spatie/QueryBuilder 应该通过请求附带的过滤器来过滤数据。我确信过滤器作为数组位于队列中,我也可以在作业中记录过滤器。然后,该工作调用我在 Github 上找到的代码https://github.com/spatie/laravel-query-builder/discussions/777

    $request = app(QueryBuilderRequest::class)->merge(['filter' => $this->filter]);

    Log::info($request->get('filter'));  // This is array('filter' => array('id' => 10))

    return QueryBuilder::for(Customer::class, $request)
        ->where('id', '<', 20)
        ->toBase()
        ->orderByDesc('id');

条件类似于filter[id]=10,但结果返回所有id < 20. How to set up the filter to the request properly to force QueryBuilder to use it. Thanks a lot

queue jobs query-builder laravel-9
1个回答
0
投票

终于知道问题出在哪里了。我忘记将 allowedFilters() 添加到 QueryBuilder

$request = new QueryBuilderRequest(['filter' => $this->filter]);

return QueryBuilder::for(Customer::class, $request)
    ->allowedFilters(..........)  // This was the problem
    ->toBase()
    ->orderByDesc('id');
© www.soinside.com 2019 - 2024. All rights reserved.