The request recorded order 1042 and dispatched the work. A job is a class that implements ShouldQueue. The constructor holds the data the worker needs later. handle() does the work. Pass the order id, not a pile of request input, so the payload stays small and obvious.
Queues in production · Lesson 1
The job class
Video
01 Pass the id and load the row in handle()
SerializesModels can reload a model for you, and it fails the job when that row has been deleted. Passing the id makes the same choice visible in handle().
class MarkOrderPaid implements ShouldQueue
{
use Queueable;
public function __construct(public int $orderId, public string $attempt)
{
}
public function handle(): void
{
$order = Order::query()->findOrFail($this->orderId);
$order->forceFill([
'status' => 'paid',
'paid_attempt' => $this->attempt,
])->save();
}
}The queue stores the constructor arguments. Do not put a card number, a token or a raw request in there. failed_jobs keeps that payload after a failure, and Horizon shows it to anyone who can open the dashboard.
02 Put payment work on its own queue
A slow report on default should not sit in front of a payment. onQueue() is called from the constructor, so every dispatch of this class uses the same name. The queue name is not part of the payload you have to remember at the call site.
class Example
{
public function __construct(public int $orderId, public string $attempt)
{
$this->onQueue('payments');
}
}A worker that only listens to default will never take the job. Locally that means queue:work --queue=payments,default. In production the Horizon supervisor has to list payments, which is the last lesson in this series. The constructor still holds the id and the attempt. Those are the only values the failed job row needs.
Recap
You can pass an id and an attempt into a job, and keep a card number out of the payload.
ShouldQueueplushandle()is the worker.SerializesModelswill reload a model, and fail if the row has gone. An id makes that choice visible.onQueue('payments')in the constructor means every dispatch uses that queue.
Check yourself · 3 questions
Question 1 of 3The constructor receives the whole request. What ends up in failed_jobs?