Saturday, 28 January 2023

How do I connect Laravel 7 to Microsoft SQL Server?



Integrating Laravel 7 with Microsoft SQL Server can significantly enhance your web application's capabilities, allowing it to leverage the robust features of one of the most powerful database management systems. This guide provides a concise and SEO-friendly overview of the steps required to establish a connection between Laravel 7 and Microsoft SQL Server, ensuring your application can interact with your database seamlessly.
Pre-requisites

Before proceeding, ensure you have Laravel 7 installed on your system. You also need access to a Microsoft SQL Server instance. The SQL Server's version should be compatible with the PHP version you're using in your Laravel project.


Step 1: Install the Required Driver


To connect to Microsoft SQL Server from Laravel, you need the correct PHP driver installed on your server. For Windows, you can use the SQLSRV extension. On Linux, you'll need to install the FreeTDS driver as Laravel uses PDO for database connections.Windows: Download and enable the SQLSRV extension in your php.ini file.
Linux: Install FreeTDS and the PHP PDO extension for SQLSRV (pdo_sqlsrv).

Step 2: Configure Your Laravel Project

After installing the necessary drivers, the next step is to configure your database connection in Laravel. This involves editing the .env file and the config/database.php file in your Laravel project..env File: Locate your .env file at the root of your Laravel project. Add or modify the following lines to match your SQL Server's credentials:

DB_CONNECTION=sqlsrv
DB_HOST=server_host
DB_PORT=server_port
DB_DATABASE=databasename
DB_USERNAME=databaseusername
DB_PASSWORD=databasepassword

config/database.php: Open this file and scroll down to the connections array. Ensure that the sqlsrv connection parameters match the ones you specified in your .env file.

Step 3: Test the Connection

To verify that your Laravel application can successfully connect to Microsoft SQL Server, you can use Laravel's built-in database tools. Running a migration is a simple way to test this:

php artisan migrate


If the migration runs successfully without errors, your Laravel application is now connected to Microsoft SQL Server.
Troubleshooting

If you encounter any issues, check the following:Ensure the SQL Server is running and accessible.
Verify your PHP driver installation and configuration.
Double-check your .env and config/database.php settings.

Additionally

We can download SQL Server extension from here download-drivers-php-sql-server

On windows if your system is 32 bit or 64 bit then download both extensions accordingly.

Add sql server extensions in php.ini as follow


For 64 bit system

extension=php_sqlsrv_74_ts_x64.dll
extension=php_pdo_sqlsrv_74_ts_x64.dll

For 32 bit system

extension=php_sqlsrv_74_ts_x86.dll
extension=php_pdo_sqlsrv_74_ts_x86.dll

To verify both extension are installed check php settings using phpinfo

No comments:

Post a Comment

Laravel csrf token mismatch for ajax post request

Error "CSRF Token Mismatch" commonly occurs if tokens do not match in both sessions and sent , and received requests.CSRF token he...