420 views
asked in Laravel by
Laravel Multiple Database configuration

1 Answer

answered by

Laravel Multiple Database configuration

Step 1: Configure Database Connections

// config/database.php

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        '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', ''),
        // ...
    ],

    'second_db' => [
        'driver' => 'mysql',
        'host' => env('SECOND_DB_HOST', '127.0.0.1'),
        'port' => env('SECOND_DB_PORT', '3306'),
        'database' => env('SECOND_DB_DATABASE', 'forge'),
        'username' => env('SECOND_DB_USERNAME', 'forge'),
        'password' => env('SECOND_DB_PASSWORD', ''),
        // ...
    ],
    // Add more database connections as needed
],

Step 2: Set Environment Variables

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

SECOND_DB_CONNECTION=mysql
SECOND_DB_HOST=127.0.0.1
SECOND_DB_PORT=3306
SECOND_DB_DATABASE=second_database
SECOND_DB_USERNAME=second_username
SECOND_DB_PASSWORD=second_password

Step 3: Create Models

php artisan make:model User -m
php artisan make:model Product -m

This instruction creates model and migration files for each model. Ensure that you define the connection in the generated model files:

// app/Models/User.php
protected $connection = 'mysql';

// app/Models/Product.php
protected $connection = 'second_db';

Step 4: Run Migrations

php artisan migrate
php artisan migrate --database=second_db

Step 5: Query Data from Multiple Databases

use App\Models\User;
use App\Models\Product;

// Retrieve users from the 'mysql' database

$users = User::all();

// Retrieve products from the 'second_db' database

$products = Product::all();

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...