Saturday, 28 January 2023

Configure Laravel With SQL Server

When developers use Laravel with Microsoft SQL Server, they can make powerful web apps that use a strong relational database system. Laravel's default database is MySQL, but it also works with SQL Server through the sqlsrv driver. It's easy to connect Laravel to SQL Server once you have the right settings and drivers installed. This guide will show you how to easily set up Laravel with SQL Server by going over the requirements, installation steps, configuration steps, and connection testing.

Understanding the Requirements:

Before you start setting things up, make sure you have the following things ready:

  • You have installed Laravel on your development machine.
  • SQL Server is either running on a local server or a remote server.
  • The drivers that PHP needs to talk to SQL Server. This is usually the SQLSRV extension for Windows. You need to install the FreeTDS and pdo_dblib extensions on Linux and macOS.

Step 1: Install Laravel

If you don't already have Laravel installed on your computer, the first thing you need to do is use Composer to make a new Laravel project. Launch your command prompt or terminal and type in the following command:

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

This command gets the most recent version of Laravel and makes a new project folder. After the installation is done, go to the project folder.

Laravel has a flexible database configuration system that lets developers connect to a variety of database engines, such as MySQL, PostgreSQL, SQLite, and SQL Server.

Step 2: Configure Database Drivers

Your PHP environment needs to have the right drivers installed so that Laravel can talk to SQL Server.

If you use Windows, you need to turn on the following extensions in your php.ini file:

extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll

You need to restart your Apache or local development server after enabling these extensions.

If you're using Linux or macOS, install the necessary packages, like FreeTDS and pdo_dblib. These drivers connect PHP and SQL Server databases.

After you install and enable the drivers, your Laravel app will be able to talk to SQL Server.

Step 3: Update .env and Database Configuration

Laravel stores environment-specific settings inside the .env file. This file contains configuration details such as database connections, mail services, and application keys.

Open the .env file located in the root directory of your Laravel project and update the database settings as shown below:
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 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,
]

This configuration tells Laravel to use the sqlsrv driver and retrieve connection
details from the .env file.

Step 4: Test the Connection

After configuring Laravel and SQL Server, the final step is to verify the connection.
Laravel provides a convenient command called Artisan migration to test the database.
connection.

Run the following command in your project directory:

   php artisan migrate

If the connection is configured correctly, Laravel will create the default migration. tables inside your SQL Server database. This confirms that your Laravel application is successfully connected to SQL Server. Conclusion Setting up Laravel to work with SQL Server is easy if you have the right drivers and The settings are set up correctly. By making the right changes to the environment, By using the .env file and making sure the database settings are correct, developers can easily add SQL Server to their Laravel apps. Companies can benefit a lot from using SQL Server with Laravel. already using Microsoft products. It offers reliability on an enterprise level. scalability and security, while also letting developers use Laravel's features of an elegant framework. You can start building powerful things once the connection is made. Laravel apps that use SQL Server databases.


No comments:

Post a Comment

.htaccess not working even though allowoverride is enabled

You're not the only one who has had the annoying problem with Apache where your file doesn't work even after you enable it. You'...