In Laravel, using multiple databases is a common requirement, especially in complex applications where you need to separate data for different purposes or handle data sharding. Laravel provides built-in support for multiple database connections, making it relatively straightforward to set up. Here's how you can use multiple databases in Laravel:
Using Laravel's .env file for configuring multiple databases is a common practice and provides a flexible approach, allowing you to easily manage database configurations based on different environments (e.g., development, staging, production). Here's how you can set up multiple database connections using .env:
DB_CONNECTION=mysql DB_HOST=your-mysql-host DB_PORT=3306 DB_DATABASE=your-mysql-database DB_USERNAME=your-mysql-username DB_PASSWORD=your-mysql-password DB_SECOND_CONNECTION=mysql DB_SECOND_HOST=your-second-mysql-host DB_SECOND_PORT=3306 DB_SECOND_DATABASE=your-second-mysql-database DB_SECOND_USERNAME=your-second-mysql-username DB_SECOND_PASSWORD=your-second-mysql-password
Replace your-second-... placeholders with the appropriate values for your second database connection.
In your config/database.php file, you can use the values from the .env file to configure your database connections dynamically:
'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST'), 'port' => env('DB_PORT'), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), // other configurations... ], 'second_database' => [ 'driver' => 'mysql', 'host' => env('DB_SECOND_HOST'), 'port' => env('DB_SECOND_PORT'), 'database' => env('DB_SECOND_DATABASE'), 'username' => env('DB_SECOND_USERNAME'), 'password' => env('DB_SECOND_PASSWORD'), // other configurations... ], ],
Replace your-second-... placeholders with the appropriate values for your second database connection.
Now, you can use the connections defined in your .env file throughout your application. For example, with Eloquent models:
namespace App; use Illuminate\Database\Eloquent\Model; class SomeModel extends Model { protected $connection = 'second_database'; }
Or when running raw queries:
$users = DB::connection('second_database')->select(...);
When running migrations or seeds, you can specify the connection using the --database option, and Laravel will use the values defined in your .env file:
php artisan migrate --database=mysql php artisan migrate --database=second_database
By using the .env file for configuration, you keep your sensitive information separate and can easily manage different configurations for various environments.
By following these steps, you can easily configure and use multiple databases within your Laravel application.
If you desire to experience the best of web development to ensure growth-centric digital transformation for your business, our full-stack web application development services are perfect for you. From e-commerce portals, CMS, ERP solutions to Chatbots, custom apps and more - our web development services include all major robust web solutions.
Are you looking for a reliable partner for your next project? Contact us today!
Contact us