I’m new to php and laravel and I’m attempting to dispatch a job when one of my models is created, but when I try to dispatch the job, I get the error “Serialization of ‘Closure’ is not allowed”, which is confusing to me because I would have thought that I needed to pass in an anonymous function to get that error.
I have an observer that watches the created event for my order model.
<?php
namespace AppObservers;
use AppModelsOrder;
use AppJobsCreateOrderTasks;
class OrderObserver
{
/**
* Handle the Order "created" event.
*/
public function created(Order $order): void
{
CreateOrderTasks::dispatch($order);
}
...etc
}
And I have my job
<?php
namespace AppJobs;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationQueueQueueable;
use AppModelsOrder;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesLog;
class CreateOrderTasks implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Order $order;
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Execute the job.
*/
public function handle(): void
{
Log::info('Order retrieved:', ['order' => $this->order]); // Log the order details
}
}
I’ve also tried passing in just the order id and updating the job e.g.
CreateOrderTasks::dispatch($order->id);
But I still get the same error. What am I doing wrong?