Lorenzo Pilati
Lorenzo Pilati

Reputation: 1

How can I pass an AbstractQueryFilter or its filters to a Job in Laravel without encountering the 'Serialization of 'Closure' is not allowed' error?

I’m using Ambengers\QueryFilter\AbstractQueryFilter (Git Repository) in a project. I need to perform a database search and generate a PDF of student logs, but I want to handle this with a job. When I try to pass an object of a class that extends Ambengers\QueryFilter\AbstractQueryFilter, I get the error "Serialization of 'Closure' is not allowed". How can I use AbstractQueryFilter to perform the database search within the job?

Here are the relevant parts of the Job class that are important for the issue:

class CreateStudentLogsPdfJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(
        public StudentLogsFilters $filters
    ){}

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $studentLogs = StudentLogs::with('user')
            ->with('discipline')
            ->where('consumer_id', DomainHelper::getConsumerId())
            ->orderBy('created_at', 'desc')
            ->filter($this->filters);
    }
}

I’ve attempted to pass just the filter instances and other related data, but without success.

I’ve tried replacing the anonymous functions with explicitly declared functions in the StudentLogsFilters class, but the issue persists.

I’m hoping to continue leveraging the benefits of AbstractQueryFilter, and if necessary, just pass the query builder it creates.

EDIT: I found out that this happens because Ambengers\QueryFilter\AbstractQueryFilter requires a Request attribute in its constructor. I'll explore other approaches.

Upvotes: 0

Views: 21

Answers (0)

Related Questions