How Laravel operates multiple databases

problem description

the project requires programs to connect to multiple databases (about 30, with the same username and password, but different library names). How to operate gracefully in Laravel?

reference

I have looked up the article Laravel model implementing multi-database query or multi-table mapping , and defined all database links in the config/databese.php configuration, but there are so many libraries that it doesn"t feel very reliable. Can I change the name of the connected database in the model?

Oct.13,2021
Only the connection name can be specified in the

model. You can write a service provider, to add configuration items to config/database.php


by default, all Eloquent models use the default database connection settings in the application. If you want to specify a different connection for the model, you can use the $connection attribute:


declare multiple links in database.php , and specify the connection name in the model

protected $connection = 'xxx';

you must declare in database.php. Then pass

protected $connection = 'connect';

or

DB::connection('connect')

to link, if the ip,username,pass are all the same, you can share these variables

Menu