Estimated reading time: 4 minutes
database.php
in config
directoryThis file is located in the directory config
of your Laravel application.
In the file database.php
is possible definish multiple database connections. Every connection must be definited as an array. The array should contain the following information:
driver
: the database driver to use;host
: the name host
or the address IP
of the database server;port
: the database server port number;database
: the database name;username
: the username for connecting to the database;password
: the password for connecting to the database;For example, the following code defiThere are two database connections, one for MySQL and one for PostgreSQL:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
After defiOnce you have database connections, you can use them in your code Laravel
. To do this, you can use the facade
of the database. There facade
database provides a unified interface for interacting with databases.
To switch between database connections, you can use the method Connection()
of facade
Databases. The method Connection()
takes the name of the database connection as an argument.
For example, the following code goes from the mysql DB to the pgsql DB:
use Illuminate\Support\Facades\DB;
DB::connection('pgsql');
Once you switch to a database connection, you can use it to query and interact with the database.
There are numerous benefits to using multiple databases in Laravel, including:
Here are some best practices for using multiple databases in Laravel:
Connection()
to go from one DB to another - this will help you avoid accidentally running Query on wrong.Using multiple databases in Laravel can be a great way to improve the performance, security, and scalability of your application. By following the best practices described in this article, you can use multiple databases in Laravel effectively.
Ercole Palmeri
The naval sector is a true global economic power, which has navigated towards a 150 billion market...
Last Monday, the Financial Times announced a deal with OpenAI. FT licenses its world-class journalism…
Millions of people pay for streaming services, paying monthly subscription fees. It is common opinion that you…
Coveware by Veeam will continue to provide cyber extortion incident response services. Coveware will offer forensics and remediation capabilities…