In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of using multiple databases in Laravel". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's way of thinking to study and learn "what is the method of using multiple databases in Laravel".
Use .env > = 5.0 (tests based on 5.5 Laravel 8 are also available)
In the .env file
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=database1DB_USERNAME=rootDB_PASSWORD=secretDB_CONNECTION_SECOND=mysqlDB_HOST_SECOND=127.0.0.1DB_PORT_SECOND=3306DB_DATABASE_SECOND=database2DB_USERNAME_SECOND=rootDB_PASSWORD_SECOND=secret
In the config/database.php file
'mysql' = > [' driver' = > env ('DB_CONNECTION'),' host' = > env ('DB_HOST'),' port' = > env ('DB_PORT'),' database' = > env ('DB_DATABASE'),' username' = > env ('DB_USERNAME'),' password' = > env ('DB_PASSWORD'),],' mysql2' = > ['driver' = > env (' DB_CONNECTION_SECOND') 'host' = > env (' DB_HOST_SECOND'), 'port' = > env (' DB_PORT_SECOND'), 'database' = > env (' DB_DATABASE_SECOND'), 'username' = > env (' DB_USERNAME_SECOND'), 'password' = > env (' DB_PASSWORD_SECOND'),]
Note: in mysql2, if db_username and db_password are the same, then you can use env ('DB_USERNAME').
Pattern
To specify which connection to use, simply use the connection () method
Schema::connection ('mysql2')-> create (' some_table', function ($table) {$table- > increments ('id'):})
Query producer
$users = DB::connection ('mysql2')-> select (...)
Model
Set the $connection variable in the model.
Class SomeModel extends Eloquent {protected $connection = 'mysql2';}
You can also define connections at run time through the setConnection method or the on static method:
Class SomeController extends BaseController {public function someMethod () {$someModel = new SomeModel; $someModel- > setConnection ('mysql2'); / / non-static method $something = $someModel- > find (1); $something = SomeModel::on (' mysql2')-> find (1); / / static method return $something;}}
Be careful when trying to establish relationships with tables across databases! You can use it this way, but it may have some considerations, depending on the database and database settings you have.
From Laravel Docs
Use multiple database connections
When using multiple connections, you can access each connection through the connection method on the DB facade class. The name passed to the connection method should correspond to a connection listed in the config/database.php configuration file:
$users = DB::connection ('foo')-> select (...)
You can also use the getPdo method on the connection instance to access the original underlying PDO instance:
$pdo = DB::connection ()-> getPdo (); thank you for reading, this is the content of "what is the method of using multiple databases in Laravel". After the study of this article, I believe you have a deeper understanding of what the method of using multiple databases in Laravel is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.