Get previous attribute value in Eloquent model event

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Retrieving Previous Attribute Values in Eloquent Model Events Body:

When working with Laravel's Eloquent ORM, you may have encountered situations where you need to compare the old value of a model attribute with its new value within saving or updating events. However, since these events are triggered as soon as changes in the database model occur, it might not always be easy to obtain the previous attribute values without using additional techniques.

The Challenges

One of the primary difficulties is that Eloquent does not directly provide methods or properties for handling model events, like saving and updating. Moreover, since Eloquent doesn't save these values to a specific attribute on its own, it can be tricky to access them without creating new instance variables or additional attributes within your application.

Possible Solutions

1. Using Listeners: An alternative approach is to use event listeners instead of model events. Event listeners are a powerful and flexible way to handle specific Laravel events, allowing you to handle the saving or updating process in your application code.

class UserListener {
    public function updating(User $user) {
        if ($user->getOriginal('username') != $user->getAttribute('username')) {
            // doSomething();
        }
    }
}

In the above example, we use event listeners to handle the updating event of a User model. By accessing the properties getOriginal() and getAttribute(), you can easily retrieve both the previous and current attribute values.

2. Creating Instance Variables:

1. Saving Event:

class User {
    public function saving(array $options) {
        // Save the old attribute values in the $oldAttributes array.
        $this->$oldAttributes = $this->getDirty();
    }
}

2. Updating Event:

class User {
    public function updating(array $options) {
        // Save the previous attribute values in the $oldAttributes array.
        $this->$oldAttributes = $this->getChanged();
    }
}

3. Using Traits: Alternatively, you could create a trait to handle the model saving and updating processes more efficiently:

trait OldAttributeSavingTrait {
    protected $oldAttributes = [];

    public function saving(array $options) {
        // Save the old attribute values in the $oldAttributes array.
        $this->$oldAttributes = $this->getDirty();
    }

    // Method for accessing previous attribute value.
    protected function getOldAttribute($attributeName) {
        return isset($this->$oldAttributes[$attributeName]) ? $this->$oldAttributes[$attributeName] : null;
    }
}

4. Using Database Events:

Conclusion

In conclusion, accessing the previous attribute values of an Eloquent model within its saving or updating event can be challenging without additional techniques, but using a combination of these approaches will allow you to effectively compare old and new values for further processing. As always, it is essential to remember proper coding practices and organization when implementing your solutions in order to maintain a clean and easy-to-understand application code base.