Update my Models

Today I've discovered a particular behaviour of Laravel models' events handling: the updating event is not fired if the object's attribute have not been really modified. More specifically, if isDirty() returns FALSE. So, your Model::updating() callback is not really executed for each save() on an existing model, but only when something changed.

This is probably desiderable in most cases (no extra events fired when not required), but not always. In my particular situation, this behaviour collided with a Model::saved() callback expecting some job to be always done by updating, and I've been haunted by a randomic bug for many months.

Unfortunately there is not a clear way to mark an object as "dirty", so the solution to enforce updating is to always change something in Model::saving() callback. The updated_at attribute (which would be anyway modified) is my choice, but eventually that's not always the right one: is up to you to identify an expendable attribute you can mess around while saving a model.

MyModel::saving(function ($object) {
  if ($object->exists)
    $object->updated_at = date('Y-m-d H:i:s');
});

MyModel::updating(function ($object) {
  // Always called at each $object->save()
});