Skip to content

Instantly share code, notes, and snippets.

@Bhavya8181
Last active July 29, 2024 11:22
Show Gist options
  • Save Bhavya8181/67e5592763bedc2a1116ca06368bb9f9 to your computer and use it in GitHub Desktop.
Save Bhavya8181/67e5592763bedc2a1116ca06368bb9f9 to your computer and use it in GitHub Desktop.
This gist file provides a comprehensive guide on how to connect a Microsoft SQL Server database to a Laravel application.
Step 1: Download the PHP Drivers for SQL Server
- Visit the [Microsoft Drivers for PHP for SQL Server](https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16)
- Download & unzip then select "php_sqlsrv.dll" & "php_pdo_sqlsrv.dll" file an appropriate version for your PHP installation.
- Copy & Paste that file and move in your ext folder inside php for ex: D:\xampp\php\ext
(Note: downloaded zip file unzip & open "SQLSRV_Readme" file to check your appropriate dll file. base on your php version)
Step 2: Update the php.ini File
Open your `php.ini` file and add the following lines:
```ini
extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll
Step 3: Restart Your Web Server
Step 4: Install ODBC Driver 17 for SQL Server
- Install the [ODBC Driver 17 for SQL Server on Windows](https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver16#download-for-windows) to ensure your system can communicate with the SQL Server database.
Configuration in Laravel
=========================
Step 1: Install Required Composer Packages in your project
- composer require doctrine/dbal
- composer require ext-sqlsrv
Step 2: Update config/database.php
Add the SQL Server connection configuration:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
Step 3: Update .env file
DB_CONNECTION=sqlsrv
DB_HOST=your_sql_server_host
DB_PORT=1433
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Replace with your actual credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment