laravel 11 multiple database connections | websolutionscode.com
laravel 11 multiple database connections | websolutionscode.com

laravel 11 multiple database connections

In web development, especially in large-scale applications, managing multiple databases efficiently is a common requirement. Laravel, being a robust PHP framework, offers elegant solutions to handle multiple database connections seamlessly. In this guide, we’ll delve into how to set up and utilize multiple database connections in Laravel 11, the latest version as of writing.

Understanding Multiple Database Connections in Laravel

Laravel’s database configuration file (config/database.php) allows you to define multiple database connections. Each connection is an array of settings that specify the database driver, host, database name, username, password, and other necessary details.

Configuring Multiple Database Connections

Let’s start by configuring multiple database connections in Laravel 11.

  1. Open config/database.php: Navigate to the config directory in your Laravel project and open the database.php file.
  2. Define Additional Connections: Inside the connections array, add new connection configurations. For example:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

'second_mysql' => [
'driver' => 'mysql',
'host' => env('SECOND_DB_HOST', '127.0.0.1'),
'database' => env('SECOND_DB_DATABASE', 'forge'),
'username' => env('SECOND_DB_USERNAME', 'forge'),
'password' => env('SECOND_DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

Environment Configuration: Update your .env file to include the new database connection details:

SECOND_DB_HOST=127.0.0.1
SECOND_DB_DATABASE=second_database
SECOND_DB_USERNAME=root
SECOND_DB_PASSWORD=secret

Utilizing Multiple Database Connections

Once you’ve configured multiple database connections, you can use them throughout your application.

  1. Model Configuration: In your Eloquent models, specify the connection to be used:
namespace App;

use Illuminate\Database\Eloquent\Model;

class SecondModel extends Model
{
protected $connection = 'second_mysql';
}

Querying the Database: To perform operations on the second database, simply use the model as usual:

$data = SecondModel::all();

Conclusion

In Laravel 11, handling multiple database connections is straightforward. By configuring additional connections in the config/database.php file and specifying the connection in your Eloquent models, you can seamlessly interact with multiple databases within your Laravel application.

Remember to always secure your database credentials and follow best practices for database interaction to ensure the security and stability of your application. Happy coding!