Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of using multiple databases in Laravel

Shulou Source: shulou.com Published: 2022-06-02 20:52:37 10月03日 Update

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!

Tags: Method data database file learning content multiple instance model original same item depends on variable just can pass name that is bottom layer train of thought Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Microsoft vpn macOS