Dave Wize's avatar

Updated Lifecycle Hook Missed

Hi Everyone.

I encountered a bug in my code. I'm triggering twice save() on the same model in the same request, but only the first time the updated lifecycle hook is actually executed. Here is a short version of my code.

class RetryTransaction
{
  public function handle(Transaction $transaction)
  {
     $transaction->status = Pending::class;
     $transaction->save()

     $response = $transaction->gateway->process()
      
      if ($response->success()) {
        $transaction->status = Approved::class
        $ransaction->save()
      }
  }

And here is my lifecycle hook.

    public function updated(Transaction $transaction): void
    {
        if ($transaction->isDirty('status')) {
            UpdatePaymentStatus::make($transaction->payment)->updateStatus();
        }
    }

Now my problem is that I see that only the first time the ->save() method is called the PaymentStatus of parent is being updated, which makes me think that the lifecycle hook is only run once in a request...

0 likes
0 replies

Please or to participate in this conversation.