Laravel Eloquent update just if changes have been made

You’re already doing it!

save() will check if something in the model has changed. If it hasn’t it won’t run a db query.

Here’s the relevant part of code in Illuminate\Database\Eloquent\Model@performUpdate:

protected function performUpdate(Builder $query, array $options = [])
    $dirty = $this->getDirty();

    if (count($dirty) > 0)
        // runs update query

    return true;

The getDirty() method simply compares the current attributes with a copy saved in original when the model is created. This is done in the syncOriginal() method:

public function __construct(array $attributes = array())



public function syncOriginal()
    $this->original = $this->attributes;

    return $this;

If you want to check if the model is dirty just call isDirty():

    // changes have been made

Or if you want to check a certain attribute:

    // price has changed

Leave a Comment