I am trying to modify several models (Laravel 5) in one of my controllers, so I receive all the info I need from a very large form, and then find the models in my database, modify them and finally save them in order to keep the changes.
But, lets say I am saving 5 different models, one after the other, and when the system is trying to save the 3rd model (for example), something goes wrong and the process ends. How can I revert the previous changes, so I don't save partial changes?
Any idea would be appreciated. Thanks.
You'd use a transaction for this.
If an exception occurs within the
DB::transaction
block, all changes within it will be rolled back.