Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uksarkar's avatar

How to create new database for every session

I have a Laravel project and I want to create a demo view for user. So, I want to generate new database with some default data for every logged-in user and destroy the database after completed the session. My idea to create new SQLite database for every user and destroy it after the session is completed. Now how can I implement to new SQLite database when a user logged-in?

0 likes
6 replies
bobbybouwmann's avatar

Well, the basic steps are as followed

  • Create a new .sqlite file for the session
  • You need to migrate and seed that database (not sure if you really want this, it might take some time while the user is logging in. You should show a loading screen then or something)
  • Change the config based on the session (you need to do this in a middleware, so it always sets the database correctly based on the session)
Config::set('database.connections.sqlite.database', database_url($sqliteFileName));
  • You need a way to remove the databases that are old and not used anymore.
1 like
uksarkar's avatar

Hi @bobbybouwmann , I want to seed some data. I will create the seeder for this. But should I create new middleware and register it for every request or can I add the functionality to Authenticate middleware?

uksarkar's avatar

Hello @bobbybouwmann , I just followed your step and everything is set up correctly. But the database is not working. I have a database.sqlite in database directory it is default for some reason, then I am creating new database in database/tamp/***.sqlite in LoginController's authenticated() method. And storing the database name by session()->put();. After that I had created a middleware and register it web middleware group's after the sessionStart middleware, and setting config like this:

    if(config('app.mode') === 'demo' && session()->has('database')) {
            config()->set('database.sqlite.database',database_path(session()->get('database')));
            // dd(config()->get('database.sqlite.database'));
        }

In the dd() I am getting the right database path but the application is using default one. Now how can I fix this?

martinbean's avatar

@uksarkar Don’t create a database on demand like this unless you want your users to crash your server.

Instead, you could have interested customers create a demo account by clicking a button. You could have a server-side script then create a new database, migrate, and seed it using a queued job. When the queued job finishes, broadcast an event to the front-end and redirect the customer to their newly-configured demo account.

Make sure to have a scheduled task to clean up your created databases.

bobbybouwmann's avatar

@uksarkar Another solution is setting up a demo database and just copying over that database whenever someone starts a new demo.

Please or to participate in this conversation.