2

I need multi database concept for my project. I create user on real time automatically create new database for that new user .that new user login switched to master database to user database.. how to do this .. I am trying larval 5.4 version .

1.How to create database on real-time for new user ?

2.How to switch users login master database to user database ?

john henry
  • 25
  • 1
  • 5

1 Answers1

1

You can create schemas and migrate them real time, I've done exactly this before in a project. Use the following

DB::connection('mydb')->statement(DB::raw('CREATE DATABASE....BLAH BLAH BLAH'))

to execute raw queries for creating new databases. You'll need your master connection to execute these queries.

Then you can use Artisan::call('migrate', ['--database' => 'newdb']) to migrate and optionally seed it.

Finally, you will need to either have a customers entry in your database config file that determines which schema the user connects to, or you can set this real time using Config::set(['my.user.db.config').

My strategy for creating unique databases for the customer was to use a guid as the database name and keep a master log of that in my master database. When a user would log in, their database name would be read from master and then the application would switch his connection on the fly. My company has hundreds of small customer databases with names like 123151-2135151-5132123-545-231231 that keep them unique.

It's quite involved and there are a lot of moving pieces but that will get you started.