In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.