I will try as much as I can to explain my question,
let's say that I have a shipping system that many admins using, one of the shipping steps is to put a selected packages in a container,
the problem: one of the admins added wrong package in one of the containers. ( this action effect more then one table in the database ), so couple models is involved in this action ( example: invoice model, container model, package model ) .
he moved on to the next task and many transations have been committed to the database ( using eloquent ) after that another admin find's out the problem,
the question: is there a way to record each step transactions ( all the involved models ) , so we can roll back when needed ?
You can not just rollback the transaction after it was being commited, even if you have recorded all the changes it makes. The reason being is that there might be further changes affecting the same records, so you now have version conflict. Some records created or modified migth not be valid anymore.
Instead, as others pointed out, you need to implement this on the application level. Let say you have a Command object (some may name it an Action or just a service), that encapsulates all the operations of adding a package to the container. Let's call it AddPackage
and it makes all the changes in the different models. Now you need to implement RemovePackage
which will do the reverse operations.
Now for practical reasons, you may need to know what was the value of a given field in a given model before the AddPackage was executed. That means that you need to keep versions of the records. In the rails world that is achieved with paper_trail
, and I'm not much of a Laravel guy, but quick search revealed the spatie/laravel-activitylog