仅在范围内覆盖默认 DB::connection

我的 Laravel 应用程序中有多个 MySQL 连接到不同的数据库。我知道我不能用 覆盖默认连接DB::connection,但我想知道是否有办法在函数的给定范围内只覆盖一次。


例如我目前:


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

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

...

...

但我希望:


DB::connection('mysql2'); // override connection once in this scope

DB::table('users')->select ...

DB::table('orders')->insert ...


慕工程0101907
浏览 191回答 1
1回答

慕侠2389804

你在找这个吗?$conn = DB::connection('mysql2')$conn->table('users')->select ...$conn->table('orders')->insert ...
打开App,查看更多内容
随时随地看视频慕课网APP