I'm creating an application in Laravel where each one will have its database, is it possible for each module to receive its connection file instead of leaving everything inside the config/database?
Asked
Active
Viewed 1,473 times
1
-
I don't know if it's possible, but why would you like to save separately? Yo can have multiple database configuration "blocks", and manage everything from your `.env` file. Thats the way I do that. – Sebastián Pérez Feb 18 '20 at 12:55
-
I think it's a bad idea to do that, you can create multiple connections and use different connection for different modules you can refer [this](https://stackoverflow.com/questions/52278456/laravel-change-connection-in-model-for-one-method/52278701) for more details. – Prateik Darji Feb 18 '20 at 12:57
-
@Sebastián Pérez because i know i will have countless connections and so i thought it would be simpler to organize, because i would already know that the connection would be inside the module itself – Nattan Feb 18 '20 at 12:59
-
you might want to look into microservices. from the looks of it you might want to try another architecture altogether. – Indra Feb 18 '20 at 13:01
-
@PrateikDarji as I said above I thought it would be the best way to organize ... I'm new to Laravel I still don't know all the best ways to work on it – Nattan Feb 18 '20 at 13:02
-
Ok, maybe you can try this workaround (not sure if it works, and also is no elegant, but can works). You can create a table in your principal database called `connections`. And save all connection values. Then, you can import \DB in your `config/databases`, and make an `array push` for each connection into the `connections` array. – Sebastián Pérez Feb 18 '20 at 13:06
-
Does this answer your question? [Laravel, change connection in model for one method?](https://stackoverflow.com/questions/52278456/laravel-change-connection-in-model-for-one-method) – Prateik Darji Feb 18 '20 at 13:19
2 Answers
1
When you say module what do you mean by that?
Laravel supports multiple db connections, and in each Eloquent model you can add property $connection to specify which database connection to use for a specific model.
class MyClass extends Eloquent {
protected $connection = 'myConnectionName';
...
}
make sure that all models that are in relations are within the same database though. Add defintion of myConnectionName in config/database.php

MaxT
- 211
- 1
- 4
-
I mean working with nwidart / laravel-modules, to create application modules within my main project .... but from what I'm seeing it won't be possible to work the way I imagined ... I'll have to create the connections in .env and config / database – Nattan Feb 18 '20 at 13:12
-
Yes, I would stick to standard Laravel has set up, any changes in the standard would mean mess in maintenance later on. Because eloquent is so tightly couple with database it is a bit of a problem setting up db connections on the side. – MaxT Feb 18 '20 at 13:23
0
Yes its possible follow the following step and let me if any query
Step 1 : Add the second db code in config->database.php bottom of mysql=>[....]
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST2', '100.10.0.100'), // note add your hostname
'port' => env('DB_PORT2', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Step 2 : Add the code in .evn file
DB_CONNECTION2=mysql
DB_HOST2=100.10.0.100 // Add your hostname
DB_PORT2=3306
DB_DATABASE2=test // add your DB name
DB_USERNAME2=testuser // add your user name
DB_PASSWORD2=test123 // add your pass
Step 3 : you can access in controller like (Note : use youe table name and fields)
$records= DB::connection('mysql2');
$records1 = $records->table('contact');
$records2 = $records1->where('mobile',$client_mob)
->where('delete',0)
->first(['id','converted']);
All the best...

A.Jain
- 60
- 8