Two laravel projects,laravel_a and laravel_b,they have their respective database database_a and database_b.
Now there is some common data,I created the third database database_common,
How do the two projects read the third database?
You need to create a extra database connections. Try like this:
In config/database.php:
'connections' => [
'common_db' => [
'driver' => 'mysql',
'host' => env('COMMON_DB_HOST', ''),
'port' => env('COMMON_DB_PORT', '3306'),
'database' => env('COMMON_DB_DATABASE', 'forge'),
'username' => env('COMMON_DB_USERNAME', 'forge'),
'password' => env('COMMON_DB_PASSWORD', ''),
'unix_socket' => env('COMMON_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
In .env:
COMMON_DB_CONNECTION=mysql
COMMON_DB_HOST=127.0.0.1
COMMON_DB_PORT=3306
COMMON_DB_DATABASE=yourDatabaseName
COMMON_DB_USERNAME=yourDatabaseUsername
COMMON_DB_PASSWORD=yourDatabasePassword
Then you can write the query like this:
$users = DB::connection('common_db')->select(...);
Ref: Database: Getting Started. Read this page carefully.