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

jmagaro88's avatar

How does a job handle a deleted model as input?

What happens if a queued job is passed an Eloquent model as input, but the model is deleted before the job gets run in the queue?

For example, I am building an eCommerce site with 5.2 where a customer can enter addresses and payment methods. A payment method belongs to an address. But if a customer tries to delete an address, rather than cascading down and deleting any payment methods that are associated with it, I soft delete the address by marking it as disabled. That way, the payment method can still be used until the customer updates the billing address associated with it.

However, if the payment method is deleted and it references an address that has been soft-deleted, I want to do some garbage collection and delete the address from the database. This doesn't need to happen synchronously, so I wrote a simple queueable job to accomplish this. The handle method looks like this:

public function handle(PaymentMethodRepository $paymentMethodRepository, AddressRepository $addressRepository)
{
    $billingAddress = $paymentMethodRepository->address($this->paymentMethod);

    if ( ! $billingAddress->enabled) {
        $addressRepository->delete($billingAddress);
    }
}

I dispatch this job in the destroy method of the PaymentMethodsController. However, if the payment method passed to the job is deleted from the database before the job gets executed in the queue, will the job fail?

I'm still developing the site so I don't have a server to deploy and test out what happens. I know that the model gets serialized to be put in the queue, but I wonder if an issue would occur when the model is restored to execute the job.

0 likes
5 replies
bobbybouwmann's avatar

I think you need to use a different approach. You should fire that job when you call the soft-delete method. This way you are always save.

jmagaro88's avatar

I can't fire the job when I call the soft-delete method to delete an address. If I delete the address immediately after soft-deleting it, that defeats the purpose. I need to delete the disabled address only when the payment method it is attached to has been deleted.

There are definitely other ways to do this. I could just make it a synchronous job or I could use some eventing (maybe that's the best way). I was just wondering if this approach with the queued job would actually work.

lara25260's avatar

Your still developing the site, so you have no way of testing ?

Surely you can make it run on local host and test?

How can you make a site and not test as you go along?

jmagaro88's avatar

I'm not aware of a way to test the queue functionality locally. I thought that could only be done on deployment

lara25260's avatar

Anything can be done locally, it' virtually is the same as deployment.

Please or to participate in this conversation.