We have already seen how to connect mysql database in laravel application. But in this tutorial, I will show you how to connect sqlsrv remote mysql database in laravel. If you do not know how to connect sql server database in laravel 9, then this example is for you. I will show you the example code of laravel 9 connect to sql server. 

We will just update the .env file to connect sql server database to laravel with some credentials. See the example code:

.env

DB_CONNECTION=sqlsrv
DB_HOST=your_db_host //like 192.168.100.100
DB_PORT=1433
DB_DATABASE=your_db_name_here
DB_USERNAME=your_db_username_here
DB_PASSWORD=your_db_password_here

 

Now you can check the connection using the below code:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{   
    public function index()
    {   
        try {
            $dbname = DB::connection()->getDatabaseName();
            return "Connected database name is: {$dbname}";
        } catch(\Exception $e) {
            return "Error in connecting to the database";
        }
    }
}

 

Read also: How To Connect Remote Database With SSH Tunnel In Laravel?

 

Conclusion

I have tried to discuss the clear concept of how to connect sqlsrv remote mysql database in laravel. Now we know how to connect sql server database in laravel 9. Hope this laravel sql server connection tutorial will help you.