I needed to do this as well for just one table, and here are the quickest ways I found for Laravel 8:
// config/database.php
'mysqllogs' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_LOGS_HOST', '127.0.0.1'),
'port' => env('DB_LOGS_PORT', '3306'),
'database' => env('DB_LOGS_DATABASE', 'forge'),
'username' => env('DB_LOGS_USERNAME', 'forge'),
'password' => env('DB_LOGS_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
To access the new database:
Method 1:
DB::connection('mysqllogs')->statement('DELETE FROM logs where office_id=1');
Method 2:
$log = new Log();
.....
$log->setConnection('mysqllogs');
$log->save();
Method 3:
$logs = Log::on('mysqllogs')->where('office_id', 1);