How to use multiple databases in Laravel

Learn how to efficiently configure and utilize multiple databases within your Laravel application

Contact us

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:

1. Update .env File:


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.

2. Configure config/database.php:


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.

3. Using Connections in Your Application:


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(...);

4. Running Migrations and Seeds:


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.


Software Outsourcing Asia Services

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.

Do you have a new project?

Are you looking for a reliable partner for your next project? Contact us today!

Contact us

Copyright © 2024.
All rights reserved. asiadev.net
TECHCONSULTI CO. LTD.

Copyright © 2024. All rights reserved. asiadev.net - TECHCONSULTI CO. LTD.

Privacy Policy | Terms of Service | Disclaimer