dimanche 11 février 2018

Laravel 5 Change Database Connection

I'm currently working on a project where I need to have several different databases. The main idea is 1 database to authenticate users and after switch to another one depending on the user's organisation.

My code looks like this at the moment:

\Config::set('database.connections.organisation', array(
                'driver'    => 'mysql',
                'host'      => \Auth::user()->organisation->db_host,
                'database'  => \Auth::user()->organisation->db_database,
                'username'  => \Auth::user()->organisation->db_user,
                'password'  => \Auth::user()->organisation->db_password,
                'charset'   => 'utf8',
                'collation' => 'utf8_general_ci',
                'prefix'    => '',
            ));

            DB::setDefaultConnection('organisation');

Unfortunately it doesn't seem to apply the new connection to all my models. Is there a way to automatically changed it everywhere? Or should I create a function in all my models to update the connection?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire