nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.52k stars 959 forks source link

Multiple Databases #121

Closed mr-Sepi0l closed 7 years ago

mr-Sepi0l commented 7 years ago

Hi,

First thanks for your work. I'd like to know if it is or will be possible to have multiple .env by modules or if each modules can be related to another databases.

Thanks

adelowo commented 7 years ago

This is tricky.. The only option I can come up with is to re-bind db.connection in the module's service provider.... But that would be a "last binder connection wins". And you back to the same problem....

Not sure of this.. Contextual bindings might solve this but you'd have to use the schema builder for dB connections.. No eloquent.

mikemand commented 7 years ago

You could create new named connections in /config/database.php (in the connections key):

...
'mysql => [
    ...
],
'myotherconnection' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],
...

(Documentation here.)

As an alternative, you can register new connections in your service provider(s) of your module(s).

Eloquent models can specify which connection they use: https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions (scroll down to Database Connection).

adelowo commented 7 years ago

@mikemand oh crap, never seen that - connection per model - while reading the docs

raushansrivastava commented 6 years ago

Is there any way to use laravel L5-Modular and apply a new database connection for each module?

nWidart commented 6 years ago

Yes, you can programmatically add configuration in laravel, should be documented in their docs.