The issue you're encountering is related to how Laravel handles job serialization and deserialization. When a job is serialized, the constructor is not called again during deserialization. Instead, the properties are directly restored from the serialized data. This means that any default values or logic in the constructor won't be re-applied when the job is deserialized from the queue.
In your case, when the job is serialized, the date property is set to null (since that's the default value), and this null value is what's stored. When the job is deserialized, the constructor is not called again, so the logic to set $this->date to today()->subDay() is not executed.
To handle this, you can move the logic to set the default value into the handle method or another method that is called after deserialization. Here's how you can adjust your job class:
// MyJobClass.php
public function __construct(
public ?Carbon $date = null,
) {
// Constructor logic is not reliable for setting defaults when using queues
}
public function handle()
{
$this->date ??= today()->subDay();
// Your job logic here
}
By moving the default value logic to the handle method, you ensure that it is executed every time the job is processed, regardless of whether it was serialized and deserialized. This approach ensures that your default logic is applied consistently.