0

Suppose there are two companies A and B. Each company has one user and when they login to their account i want to switch connection to their actual database. i.e If user "John" belongs to company A and "Rambo" belongs to company B. If John is logged in i want to switch the database to company A's database. Both company's database structure is same. So I'm trying to create separate database for each company. How to achieve this.

I have added another mysql connection in database.php and also entered its credentials in .env file. What is the next step? Can someone help me!

The user table looks something like this.

Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('company')->default('A');
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
Devender Gupta
  • 496
  • 5
  • 22
  • You're probably going to have to adjust the `protected $connection ...` setting of each `Model` on the fly (or following a successful Auth action). Likely too broad an issue currently though. – Tim Lewis Aug 14 '18 at 17:28
  • I didnt understand ! Can u explain with an example? – Devender Gupta Aug 14 '18 at 17:40
  • Each Model can have a `protected $connection = "...";` attribute, that matches one of the connections defined in `config/database.php`. You'd have to override that based on the logged in User, like `protected $connection = auth()->user ? auth()->user->db_connection : "default";`. It's a bit broad, which is why I didn't post the whole explanation. The linked question somewhat describes this process too. – Tim Lewis Aug 14 '18 at 18:22

0 Answers0