Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Čamo's avatar
Level 3

How to send request object to the job queue

I have a controller which dispatch job to queue.

    public function index(Request $request): JsonResponse
    {
        $userId = auth()->id();

        ExportCustomersJob::dispatch($request, $userId);

        return $this->sendJsonResponse(...]);
    }

I am using Redis for the queue. But as I see the json stored in Redis does not contain params from dispatch() method. Can somebody tell me please how to do it?

0 likes
10 replies
rodrigo.pedra's avatar

How is your job's class constructor declared?

Job::dispatch() will delegate the parameters to the class constructor.

1 like
Čamo's avatar
Level 3

@rodrigo.pedra this is Job constructor

    public function __construct(Request $request, int $customer_id)
    {
        $this->export = $request;
        $this->customer_id = $customer_id;
        Log::info('__construct JOB');
        Log::info($request);
        Log::info($customer_id);
        Log::info('__construct JOB END');
    }
rodrigo.pedra's avatar

@Čamo as you already figured out with @sabonzy's help, the request object isn't serialized as you'd expect.

Are you using Laravel 11? You can try making use of the Context object:

https://laravel.com/docs/11.x/context

Otherwise, pass the array of filters you need into the job's constructor.

1 like
Sabonzy's avatar

The purpose of queue jobs is to be independent and stateless. Without relying on the status of the active HTTP request, you need to possess all the information required to execute. you can pass the data you need to the job like so $data = $request->only(['name', 'email']); or $data = $request->all();

1 like
Čamo's avatar
Level 3

@Sabonzy I dont agree. The purpose of job is to push long time jobs to the queue and run it as asynchrone job.

Čamo's avatar
Level 3

@Sabonzy But ok it does not matter. I need only some filters from the request object so I can send it as array. But how?

Sabonzy's avatar

@Čamo the way queues operate in Laravel, queue jobs do not by default have access to the HTTP request object. A job is serialized and then unserialized for processing when it is sent to a queue. the HTTP request object contains a large amount of dynamic and non-serializable data, such as file handles, session data, and server context.

Extract the specific data you need from the request and pass it to the job.

1 like
Čamo's avatar
Level 3

@Sabonzy Yes you are right. The request object contains some closures which can not be serialized.

Snapey's avatar

@Čamo use $request->only(), or $request->all()

Don't try queuing the complete request object

1 like

Please or to participate in this conversation.