Search code examples
phplaraveldatabasetransactionsrollback

test account db rollback after logout / session ended laravel


I would like to create a public test application, where users can have the feel of editing / deleting all data. But after they either log out or their session ends, all previous changes rolls back, so the database will be the same if another test user logs in.

Do you have any idea how I can achieve this?

What I tried so far, but not working (I guess DB::begintransaction cant have influence for all following transaction fired by other functions):

protected function authenticated(Request $request, $user)
{
    DB::beginTransaction();
    ...

public function logout(Request $request)
{
    DB::rollBack();
    ...

Thanks for all help in advance!


Solution

  • Clone the database and drop it when session ends. You could write a batch that you can call from laravel with the cloning script ( for example: https://makandracards.com/makandra/1605-mysql-how-to-clone-a-database ) and when there is no more need of it you just drop it.