Step 1
Go to config/database.php and update like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
'mysql_2' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_2', '127.0.0.1'), 'port' => env('DB_PORT_2', '3306'), 'database' => env('DB_DATABASE_2', 'forge'), 'username' => env('DB_USERNAME_2', 'forge'), 'password' => env('DB_PASSWORD_2', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], |
Step 2
Then make connection in any controller like this
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class YourController extends Controller { public function index(Request $request){ $db = DB::connection('mysql_2'); } } |
Leave a reply