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

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