ma77c
ma77c

Reputation: 1086

Laravel | Override default DB::connection only once inside scope

I have multiple MySQL connections to different databases in my Laravel application. I understand that I cannot override the default connection with DB::connection , but I am wondering if there is a way to override this only once within a given scope of a function.

For example I am currently:

DB::connection('mysql2')->table('users')->select ...
DB::connection('mysql2')->table('orders')->insert ...
...
...

But I wish to:

DB::connection('mysql2'); // override connection once in this scope
DB::table('users')->select ...
DB::table('orders')->insert ...

Upvotes: 0

Views: 96

Answers (1)

peterm
peterm

Reputation: 92815

Are you looking for this?

$conn = DB::connection('mysql2')
$conn->table('users')->select ...
$conn->table('orders')->insert ...

Upvotes: 1

Related Questions