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

guizo's avatar
Level 5

Observer column update isDirty or wasChanged

I'm using a observer to do some things when a specific column from the model is updated.

To do so I used the method isDirt() and it worked. But I saw in the documentation the method wasChanged with the same description, parameters and return value. The name wasChanged makes much more sense for me but unfortunately does not work as expected. The difference between isDirty and wasChanged is that the first calls getDirty() and the latter getChanges().

Examining the code I can't understand when wasChanged could be used. Just curious.

Here are the methods: https://laravel.com/api/master/Illuminate/Database/Eloquent/Model.html

0 likes
9 replies
guizo's avatar
Level 5

@Snapey I believe that mutators are used to manipulate the attribute value when the value is being set. That is not my case. My goal is to invoke some functionality when the value is updated like sending an notification or updating a database record in another table. Thank you.

Snapey's avatar
public function setThingAttribute($value)
{
    if(!$value === $this->attribute['thing'] {
        new Event('myThingWasChanged');
    }

    $this->attribute['thing'] = $value;
}

3 likes
guizo's avatar
Level 5

I understand that there are ways to achieve this other then using a observer. Thank you for pointing out using a mutator as one of them but personally I prefer to use an observer for this as I'm not mutating the attribute value. My doubt at this moment is more about the difference between isDirty and wasChanged methods.

D9705996's avatar

From the API docs and code

isDirty

Determine if the model or given attribute(s) have been modified.

    public function isDirty($attributes = null)
    {
        return $this->hasChanges(
            $this->getDirty(), is_array($attributes) ? $attributes : func_get_args()
        );
    }

wasChanged

Determine if the model or given attribute(s) have been modified.

    public function wasChanged($attributes = null)
    {
        return $this->hasChanges(
            $this->getChanges(), is_array($attributes) ? $attributes : func_get_args()
        );
    }

looking at the comments for the above

getChanges - Get the attributes that were changed. getDirty - Get the attributes that have been changed since last sync.

So the difference is related to whether the attributes have been synced or not

Snapey's avatar

My understanding is that a dirty model needs saving but a changed model has been saved with new values.

1 like
Nezar's avatar

isDirty (and getDirty) is used BEFORE save, to see what attributes were changed between the time when it was retrieved from the database and the time of the call, while wasChanged (and getChanges) is used AFTER save, to see that attributes were changed/updated in the last save (from code to the database).

18 likes

Please or to participate in this conversation.