Home > Software design >  How to change Eloquent Builder's connection?
How to change Eloquent Builder's connection?

Time:07-20

I have a Eloquent Builder and two database connections(mysql_server1, mysql_server2) as below:

$builder = User::query();

Now how can I change the connection of $builder?

Any helps would be appreciated

CodePudding user response:

You can try something like that

$builder = (new User())->on('mysql_server1')->query();

CodePudding user response:

Using setConnection

$builder = (new User)->setConnection('mysql_server2')
  • Related