How do I connect Laravel 7 to Microsoft SQL Server?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Simplifying Laravel 7's Microsoft SQL Server Configuration Process
Introduction: Connecting your Laravel application to a Microsoft SQL Server can be an intimidating task, especially if you are new to the platform or don't have solid experience with configuring databases. This comprehensive blog post aims to address the most common issues and provide clear instructions on connecting your Laravel 7 project to an MS SQL Server database.
1. Determine the Connection Details: Before anything else, gather the necessary details required for the connection:
- Database Port (usually 1433 for default installation)
- Host URL/IP Address (if running it on a remote server, use that instead of localhost)
- Database Name
- The user account with administrative privileges (preferably 'sa') and its password
2. Connecting to Microsoft SQL Server: In Laravel 7, you will need to define your connection in the database configuration file (database.php). Follow these steps:
```php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'), // if using env()
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE', 'test_db_1'),
'username' => env('DB_USERNAME', 'sa'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
```
3. Configuring Laravel's .env File: The environment file (.env) is where you can set your application globally. Here is how to define your database connection variables:
DB_CONNECTION=sqlsrv
DB_HOST=[your host - could be localhost or 127.0.0.1]
DB_PORT=[port of your SQL server - could be 1433 by default]
DB_DATABASE=[database name you created earlier]
DB_USERNAME=[user account with administrative privileges, i.e., 'sa']
DB_PASSWORD=[password for the user account 'sa']
If the above connection details are not in your .env file yet, create them:
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test_db_1
DB_USERNAME=sa
DB_PASSWORD=mypassword
4. Testing the Connection: Before moving further, run your application and test your connection. If you created a new database named 'test_db_1', create a table called 'articles' with columns 'id,' 'title,' 'body,' and 'created_at':
```sql
CREATE TABLE articles (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
```
Now try adding a new article in your Laravel application and check if the database is being used. If not, review your connection settings again and ensure you have followed the steps correctly.
5. What 'forge' Means: The term 'forge' refers to a separate application created by Forge, an automated deployment platform for Laravel applications. It provides functionality like managing server configurations, deploying applications, and database management. It is not directly related to your Laravel 7 project or Microsoft SQL Server configuration, so ignoring this parameter should not affect the connection process.
Conclusion: With a clear understanding of these steps and some practice, you can confidently connect your Laravel application to your Microsoft SQL Server in no time. Remember, if you encounter issues, refer back to this blog post for guidance or seek help from other Laravel developers. And if you are looking for a reliable resource on Laravel development, check out https://laravelcompany.com/blog/.