Connecting the MSSQL Server Connection with Multiple Databases in Laravel
Step 1:- Setup Laravel with Multiple Databases
Read My Previews blog link —
and flow step for setup multiple Databases project then use this blog
Step 2:- add server depending or extension of MSSQL SERVER
Step 2.1 Install Extension of MS Server and drivers
Please Read and flow the link
Step 2.2 After Install PHP Run Listed Commend
sudo apt-get -y install unixodbc-dev
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.4/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.4/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 7.4 sqlsrv pdo_sqlsrv
sudo service apache2 restart
Step 2.3 Install Command line mssql-tools
sudo apt-get install mssql-tools
sudo ls /opt/mssql-tools/bin/sqlcmd*
sudo ln -sfn /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd
Related Link :-
https://sqlserveronlinuxbackup.com/sqlcmd-command-not-found-ubuntu/
Step 2.4 Install Command line CLI
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
# Register the Microsoft Ubuntu repository
sudo apt-add-repository https://packages.microsoft.com/ubuntu/16.04/prod
# Update the list of products
sudo apt-get update
# Install mssql-cli
sudo apt-get install mssql-cli
# Install missing dependencies
sudo apt-get install -f
Step 3 Added multiple Databases connections (default existing )array
file Path :- app/config/database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL_sqlsrv'),
'host' => env('DB_HOST_sqlsrv', 'localhost'),
'port' => env('DB_PORT_sqlsrv', '1433'),
'database' => env('DB_DATABASE_sqlsrv', 'master'),
'username' => env('DB_USERNAME_sqlsrv', 'sa'),
'password' => env('DB_PASSWORD_sqlsrv', 'Bitcot@123'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
added New Variable in .env file
DB_CONNECTION_sqlsrv=sqlsrv
DB_HOST_sqlsrv=127.0.0.1
DB_PORT_sqlsrv=1433
DB_DATABASE_sqlsrv=master
DB_USERNAME_sqlsrv=sa
DB_PASSWORD_sqlsrv=xxxxxxx
Step 4:- SQL Statements
$sql = "SELECT * FROM tablesname ";
return DB::connection('sqlsrv')->select($sql);
Others reading:-
# Multiple site with use one auth and session in Laravel
Other Related Blog :-