0

I want to know about the laravel multiple databases. is it possible to use a default database which use only user login and after login separate group by group and every group use independent database. such as 'db' is the default database it's only for the all user login.

Example: Now 'John' is login using default database 'db'. John is the member of group1 after login john use 'db1' where stored John's all type of data. Other side Now 'Alex' login using default database 'db'. Alex is the member of group2 after login Alex use 'db2' where stored Alex's all type of data. After login default db connection no need so i want to replace 'bd' to 'db1' or 'db' to 'db2'. Please provide code for laravel

2

2 Answers 2

1

Define a separate database connection in config/database.php.

'mysql' => [ // default
    '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', ''),
    ...
],
'db1' => [ // another 
    'driver' => 'mysql',
    'host' => env('DB_HOST_ONE', '127.0.0.1'),
    'port' => env('DB_PORT_ONE', '3306'),
    'database' => env('DB_DATABASE_ONE', 'forge'),
    'username' => env('DB_USERNAME_ONE', 'forge'),
    'password' => env('DB_PASSWORD_ONE', ''),
    ...
]

Note that you have to define respective config in your .env.

Then when you want to use db1 connection it, use Config::set('database.default', 'db1'). However, it only works when you have known amount of database connections(that you can define in config/database.php), if you have unknown amount of databases, then you should change the config directly instead of the name of the connection only.

Example:

Config::set('database.connections.mysql.database', 'db1')
Config::set('database.connections.mysql.username', 'admin');
Config::set('database.connections.mysql.password', 'secret');

You can see my another answer to know how does it work underhood.

Sign up to request clarification or add additional context in comments.

Comments

0

In this case, I presume you are building a system that is connecting to various existing databases possibly from different applications.

You can define as many DB connections as you want in your config/database.php

'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', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],

'mysql2' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST_2', '127.0.0.1'),
    'port' => env('DB_PORT_2', '3306'),
    'database' => env('DB_DATABASE_2', 'forge'),
    'username' => env('DB_USERNAME_2', 'forge'),
    'password' => env('DB_PASSWORD_2', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],
...

You would however also need a way to specify for each user which other external DB from which their details are going to be fetched from, for instance a column db_name on the users table.

At the point of fetching the data of say 'Alex' User model, you can do something like

$user = User::find(1); //if Alex has user_id 1 and where $user->db_name is 'mysql2' as is in the config/database.php file

$userDetails = DB::connection($user->db_name)->where('username',$user->name)->where('other_details','some details')->get();

Make sure to specify this in your .env too, to match what's defined in config/database.php

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

DB_HOST_2=127.0.0.1
DB_PORT_2=3306
DB_DATABASE_2=db2_name_here
DB_USERNAME_2=db2_username_here
DB_PASSWORD_2=db2_password_here

...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.