Enable MARS in Laravel - php

For my Laravel application, I want to use MARS to connect to our SQL server. However, the documentation makes no note of this, nor is there any info on how to pass extra config parameters to the connection.
I'm on Laravel v7.30.
The database works fine apart for the few cases where I need to run several queries on one connection. Right now, those throw a "Protocol error in TDS stream" error. I'm fairly certain this is due to MARS not being activated. If, however, this could have a different reason I'm of course open to suggestions!
My config/database.php entry for SQL Server looks like this:
'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,
'appname' => env('DB_APPNAME', 'MyAppName-' . env('APP_ENV')),
],
Apart from that, the database connection is working fine and queries perform as expected.

Related

How to setup Laravel using a shared host with sqlsrv?

I have a project developed in Laravel and I want to upload the project to a Linux shared hosting.
In my project I have 2 connection to different database. First is mysql database that is in shared hosting and works well. Second is an sql server database that access to another server. So for that second database I use sqlsrv driver but I having problems to put this working at linux shared hosting, returns me this error:
SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
at PDO->__construct('dblib:host=IP:1433;dbname=DBNAME;charset=utf8', 'USER', 'PASSWORD', array(0, 2, 0, false))
I set the connection like this:
'DB2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'IP'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'DBNAME'),
'username' => env('DB_USERNAME', 'USER'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
],
Before this error I have a lot of another errors because this connection, so I have to add some extensions to php and update to 7.2 version. I also add some PHP pear packages .
The next image show you the extensions that I add:
In my local machine I can run the project without any problem and both connection working fine.
How can I solve that?
You should define DB_HOST and DB_PORT environment variables inside .env file for sqlsrv driver. Currently, it looks like the default values are used for the connection. (Default value - is the second parameter of env() function).
P.S. Please note, if you use two different connection - variable names should be different, for example:
.env content:
...
MYSQL_DB_HOST=0.0.0.0
MYSQL_DB_PORT=3939
SQLSRV_DB_HOST=0.0.0.0
SQLSRV_DB_PORT=1433
...
config/database.php content:
...
'DB' => [
'driver' => 'mysql',
'host' => env('MYSQL_DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3939'),
'database' => env('DB_DATABASE', 'DBNAME'),
'username' => env('DB_USERNAME', 'USER'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
],
'DB2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'DBNAME'),
'username' => env('DB_USERNAME', 'USER'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
],
...
I think first of all check your hosting server php version and try to put correct PDO driver for PHP version.
if you use a PHP Version 7.1.7 for *86 Architecture then put that .dll file.
extension=php_pdo_sqlsrv_71_ts_*86.dll
and if you use another Version of PHP then try to put a correct .dll file and also declare into php.ini file.
and restart your service and run it...
I hope it would help you...

Laravel 5.6 Unable to connect MS SQL Server 2008 R2 from PHP/Laravel

I am using Laravel 5.6, and trying to connect with MS SQL Server 2008 R2, all running on local machine. I have a test database named "ItemMaster", which is working fine with my C#.Net application.
When I try to connect same database from Laravel, I am getting this error:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. (SQL: select * from ITEM_CATEGORY)
Can anybody suggest how to fix this.
In config/database.php, I have setup sqlsrv driver as follows:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'MY-PC\MYSQL2008R2'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'ITEMMASTER'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
],
Getting error on this line inside my controller:
$items = DB::connection('sqlsrv')->select('select * from ITEM_CATEGORY');
I installed Microsoft Drivers 4.3 for PHP for SQL Server and copied file php_pdo_sqlsrv_71_ts_x86.dll to php/ext folder.
I find solution for this problem, sharing might help someone else making same mistake.
Actually the mistake is how I defined SQL Connection parameters in config/database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'MY-PC\MYSQL2008R2'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'ITEMMASTER'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
],
In this code segment, I actually defined my parameters as second parameter to env() which (be-definition) is not the real value but will be used as default value if that specific key is not found in .env file.
You can read more about Environment configuration here
So the final code segment should be like this:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => 'MY-PC\MYSQL2008R2',
'port' => '1433',
'database' => 'ITEMMASTER',
'username' => 'myuser',
'password' => 'mypassword',
'charset' => 'utf8',
'prefix' => '',
],
Or another alternative may be to define these parameters in .env file, in the root directory of your application .

How to make schema of postgres sql dynamic in laravel?

I need to make schema of postgres dynamic in laravel. The database config is below
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'stoddart'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'port' => env('DB_PORT', '7373'),
'prefix' => '',
'schema' => 'cp_bn',
],
Here you can see i have specified schema, but i want it to be Dynamic. The schema name will come from client side.
I am currently doing like following
$connection =DB::connection()->getPdo();
$connection->prepare("Set search_path to {$schema}")->execute();
But in this case i have to execute this whenever the connection to the database is made. I need to save it globally for all connection once it is set.

Using multiple databases for different endpoints

I have API written in lumen(laravel). I am using Eloquent for my models.
What I need to do is to use different databases based on url (endpoint).
For example I have http://apiprovider.com/api/v1/ as base API url and it connects to the api_v1 database, but I need to use another database if v2 is used http://apiprovider.com/api/v2 for instance api_v2 database.
All classes and laravel application should be the same, only different database according to version.
Database settings is stored in .env file.
Please suggest the right way to implement this ? Or at least possible ways.
Thanks.
It's just an idea, i haven't tried it but a simple way would be switching between the two databases from a middleware.
For example you could define the two connections available in database.php :
'connections' => [
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'localhost'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Then create a middleware and switch the DB in the handle method
public function handle($request, Closure $next)
{
//check the request URL and decide what DB to use
//set the DB for this request
Config::set('database.default', $dbname );
}
I think that Config::set('database.default', $dbname); will work only for the current request, but it would do what you need

Laravel different config/database.php for production and development

I'm developing Laravel app on my local server where I have database and also I'm putting in online in production environment.
Each environment has different db connection information. So far I dealt with this just commenting information when I'm committing like this:
'mysql' => [
'driver' => 'mysql',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
// 'host' => env('DB_HOST', 'localhost'),
// 'database' => env('DB_DATABASE', 'forge'),
// 'username' => env('DB_USERNAME', 'forge'),
// 'password' => env('DB_PASSWORD', ''),
// 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
but this is sure wrong approach. How can I set different database connection information for each environment is some better way?
Use the .env file instead.. Have a .env in your production and one in your development..
Take a look at your .env.example
Laravel Environment Configuration Documentation

Categories