Laravel-auditing: Q: Best practices for restoring previous state of Auditable model

Created on 4 Nov 2017  路  16Comments  路  Source: owen-it/laravel-auditing

| Q | A
| ----------------- | ---
| Bug? | no
| New Feature? | probably
| Framework | Laravel
| Framework version | 5.5.17
| Package version | 4.1.3
| PHP version | 7.1.4

Question

Is there a halfway elegant way for programmatically restoring an old state (i.e. using e.g. the third Audit of a model and applying its diff to the model), like either a Laravel framework or an Auditing function?

I'm looking to do something like:

$user->rollback($user->audits->first());

or

$user->rollback(Carbon::createFromDate($year, $month, $day));

If not, would the maintainers consider this a core function of this package or should it be put into an external library?

All 16 comments

Hi @Harti,

That feature has been implemented on master a few days ago, and will be officially available in the next major version (5).

I'm still to update the documentation (will be the very last thing), although the usage of the new method is quite straightforward.

You just have to pass an Audit corresponding to the model state you want to transition to.

I'll probably rename the method and update some logic, but the basic functionality is already there, so give it a go if you want 馃槈

Yay, go you! Awesome stuff, keep up the good work.
I'm actually amazed as to how simple this was to set up altogether, and will probably give the master snapshot a try!

While you're at it, maybe consider adding boolean $peek = false to the method's signature so that it doesn't perform a save (thus creating a new Audit immediately). Unfortunately, I'm not very skilled at making PRs so I'll have to leave this to you 馃榿

That's not a bad shout. Thanks for the suggestion @Harti!

Now it's in version 7. Some update to restore an old state?

Nothing has changed in that regard. You can use the feature as always.

Sorry, I was not clear on my question, but how to restore an old state?

I did not find anything in the documentation.

You probably didn't look hard. It's right here.

Thank you. I was looking for Restore, Rollback...

The transitionTo() method updates model attributes without persisting to the database. Calling save() on the model

Okay, I understand, but I noticed that in the database the event shows Updated and not Restored. $model->setAuditEvent('restored'); Should I use this before saving the model? I tried but without success.

The restored event is when you restore a soft deleted model. When transitioning, you're not restoring, you're just updating.

I kind of agree that it would be interesting to add a reverted event for such cases. Sounds like a feature request though.

Not really. From a database standpoint, you're just updating a model.

It would be good to indicate as Reverted. It's that I think of revisions as it is in Wikipedia.

workingtwinkle

@quetzyg From a use case standpoint, you want to use this library to convey what happened to the record, and being able to read "this was reverted to revision 42 on May 12th of 2018" is more useful or concise than "this was updated on May 12th of 2018" and having to find out what changed yourself. The information is all available once you start the transition, but it's some work to do with overriding the update event, I agree.

Like stated in the documentation:

It's the developer's responsibility to check that the new state is correct and calling save() on the model.

So once you call save() it's just gonna be a regular update, from the package/database point of view and from the user point of view.

Also, why would manually updating a property to a value it had before + saving would be considered anything different from transitioning to a state (which is just a fancy update) where the only change was that same property with the same value you manually reverted?

It would be good to indicate as Reverted. It's that I think of revisions as it is in Wikipedia.

@eduardodallmann, the scope of this package is to track model changes, not make model changes.

It doesn't create/update/delete/restore and certainly doesn't revert. That's something the programmer does.

Was this page helpful?
0 / 5 - 0 ratings