skip to Main Content

I have the following code where I fire an event comparing two versions of the same model.
I worked around it by manually setting a "dirty" attribute.
However, is there a way to fetch the whole original model on updated event?

class NovaSubscriptionObserver
{
    public function updated(Subscription $subscription): void
    {
        if ($subscription->isDirty('price_id')) {
            $oldSubscription = $subscription;
            $oldSubscription->price_id = $subscription->getOriginal('price_id');
            SubscriptionCreatedOrUpdatedEvent::dispatch($subscription, $oldSubscription);
        }
    }
}

3

Answers


  1. You can use:

    $originalModel = $subscription->fresh();
    
    Login or Signup to reply.
  2. You can use:

    $orignalColumns = $subscription->getOriginal();
    

    This will return the column values that your model had when it was first loaded from the database.

    Login or Signup to reply.
  3. You can try:

    $orignalColumns = $subscription->getRawOriginal();
    

    This will fetch the model’s raw original attribute values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search