How To Change The Database Connection Mid Code In OctoberCMS / Laravel

- 1 answer

I am trying to create one system that would work for multiple customers, each with their own database but using the same online system.

The summery is, when a user is logged in I would only select the database of his company and would only see data on his database.

if this way is not recommend, Give me your feedback.



Use the Config facade to change the connection. You will need to ensure that models that use a common database (i.e. user authentication) have their database connections set explicitly so that the dynamic changes in the configuration don't affect those models.

Here's an example of how you would accomplish this.

In your custom plugin's Plugin.php file:

use Config;
use Backend\Models\User;
use System\Classes\PluginBase;

class Plugin extends PluginBase
    public function register()
        User::extend(function($model) {

        Config::set('database.default', 'this_companies_connection');