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?
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)
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?
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:
@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.