Saturday, 28 January 2023

Configure Laravel With SQL Server

Understanding the Requirements:

Before diving into the configuration, ensure you have the following prerequisites in place:

  • Laravel is installed on your development machine.
  • SQL Server running either locally or on a remote server.
  • The necessary drivers for PHP to communicate with SQL Server. For Windows, this is usually the SQLSRV extension. For Linux and macOS, you'll need to install the FreeTDS and pdo_dblib extensions.

Step 1: Install Laravel

If you haven't already installed Laravel, begin by creating a new Laravel project via Composer:

composer create-project --prefer-dist laravel/laravel YourProjectName

Step 2: Configure Database Drivers

Ensure your development environment has the required drivers to connect PHP with SQL Server:

Step 3: Update .env and Database Configuration

Modify your project's .env file to include your SQL Server database connection details:

DB_CONNECTION=sqlsrv
DB_HOST=localhost\SQLEXPRESS
DB_PORT=
DB_DATABASE=databasename
DB_USERNAME=sa
DB_PASSWORD=123456


Here is an example screenshot below




Open the file config/database.php and make sure you will have the following setting
in your database.php file.
'sqlsrv' => [
    'driver' => 'sqlsrv',
    'url' => env('DATABASE_URL'),
    '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 4: Test the Connection
To check and verify the database drivers we need to run the migration command.

php artisan migrate

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

Laravel Rest API Localization


We can easily do Laravel rest api localization with the help of middleware,

with the help of API translation, we can translate all API responses into different international

languages.



1 - Creating Language Middleware

For example:

We will create ApiLocalization Middleware.

Run the Artisan command to generate the middle.


php artisan make:middleware ApiLocalization



After running the above command in Laravel Artisan a new middleware will be created under


path: your_project_folder\app\http\middleware.


We need to update the ApiLocalization middleware which will check the

X-localization in the request.


$local = ($request->hasHeader('X-localization')) ? $request->header('X-localization') : 'en';


After checking the required header in the request, it will then set the local


app()->setLocale($local);







This is how we can switch between different languages in laravel using middleware.

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...