laravel 如何使用查询构建器 DB::table(..) 和 DB::connection()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26181170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:14:26  来源:igfitidea点击:

laravel how to use query builder DB::table(..) with DB::connection()

phplaravel

提问by Phil

I have 2 databases defined in config/databases, mysql and mysql2
The default connection is mysql

我在 config/databases 中定义了 2 个数据库,mysql 和 mysql2
默认连接是 mysql

I need to get this data from mysql2

我需要从 mysql2 获取这些数据

$programs=DB::table('node')->where('type', 'Programs')->get();

The docs tell me I can change the connection using

文档告诉我我可以使用更改连接

$programs=DB::connection('mysql2')->select(...)

which would let me run a sql statement to get an array for $programs. But I am wondering if there is a way to combine the 2 statements i.e. use query builder on specific db::connection.

这将让我运行 sql 语句来获取 $programs 的数组。但我想知道是否有一种方法可以组合这两个语句,即在特定的 db::connection 上使用查询构建器。

回答by Marcin Nabia?ek

You should use:

你应该使用:

$programs=DB::connection('mysql2')->table('node')->where('type', 'Programs')->get();