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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b357c0-2bfb4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b357c0-2bfb4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?