Čamo's avatar
Level 3

Spatie QueryBuilder in Job queue

I have a Job which runs in queue. There is a Spatie/QueryBuilder which should filter data through the filter which came with the request. I am sure the filter is in the queue as array and I also can log the filter inside the job. The job then call this code which I found on 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 OK

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

The condition is like filter[id]=10 but the result returns all ids < 20. How to set up the filter to the request properly to force QueryBuilder to use it. Thanks a lot

EDIT: It seems the QueryBuilder get the filters from query() instead of input(). But dont know how to set up query InputBag

0 likes
1 reply
Čamo's avatar
Level 3

Finaly I found out what was the problem. I forgot to add allowdFilters() to the QueryBuilder

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

        return QueryBuilder::for(Customer::class, $request)
            ->allowedFilters(..........)  // This was the problem
            ->toBase()
            ->orderByDesc('id');

Please or to participate in this conversation.