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.
Related
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.
I have secondary connection that comes dynamically and here is it's code
'userdb' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => DYNAMIC_DATABASE_NAME,
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
],
So, once use logged in I want to update update this database dynamically and reconnect to this userdb.
I have used following code but this is not giving persistent DB connection I have to this this everytime manually.
Any suggestions ?
DB::purge('userdb');
config(['database.connections.userdb.database' => 'test']);
DB::reconnect('userdb');
DB::connection('userdb');
1.first set database name
\Config::set('database.connections.userdb.database','test');
2.after set database name you need to purge the DB cache
\DB::purge('userdb');
3.set default connection after u purge db cache
\DB::setDefaultConnection('userdb');
I want to know about the laravel multiple databases. is it possible to use a default database which use only user login and after login separate group by group and every group use independent database. such as 'db' is the default database it's only for the all user login.
Example: Now 'John' is login using default database 'db'. John is the member of group1 after login john use 'db1' where stored John's all type of data. Other side Now 'Alex' login using default database 'db'. Alex is the member of group2 after login Alex use 'db2' where stored Alex's all type of data. After login default db connection no need so i want to replace 'bd' to 'db1' or 'db' to 'db2'. Please provide code for laravel
Define a separate database connection in config/database.php.
'mysql' => [ // default
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
],
'db1' => [ // another
'driver' => 'mysql',
'host' => env('DB_HOST_ONE', '127.0.0.1'),
'port' => env('DB_PORT_ONE', '3306'),
'database' => env('DB_DATABASE_ONE', 'forge'),
'username' => env('DB_USERNAME_ONE', 'forge'),
'password' => env('DB_PASSWORD_ONE', ''),
...
]
Note that you have to define respective config in your .env.
Then when you want to use db1 connection it, use Config::set('database.default', 'db1'). However, it only works when you have known amount of database connections(that you can define in config/database.php), if you have unknown amount of databases, then you should change the config directly instead of the name of the connection only.
Example:
Config::set('database.connections.mysql.database', 'db1')
Config::set('database.connections.mysql.username', 'admin');
Config::set('database.connections.mysql.password', 'secret');
You can see my another answer to know how does it work underhood.
In this case, I presume you are building a system that is connecting to various existing databases possibly from different applications.
You can define as many DB connections as you want in your config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
...
You would however also need a way to specify for each user which other external DB from which their details are going to be fetched from, for instance a column db_name on the users table.
At the point of fetching the data of say 'Alex' User model, you can do something like
$user = User::find(1); //if Alex has user_id 1 and where $user->db_name is 'mysql2' as is in the config/database.php file
$userDetails = DB::connection($user->db_name)->where('username',$user->name)->where('other_details','some details')->get();
Make sure to specify this in your .env too, to match what's defined in config/database.php
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_HOST_2=127.0.0.1
DB_PORT_2=3306
DB_DATABASE_2=db2_name_here
DB_USERNAME_2=db2_username_here
DB_PASSWORD_2=db2_password_here
...
I am getting following error -
could not find driver (SQL: select * from [customer] order by [id] desc)
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'Customer'),
'username' => env('DB_USERNAME', 'SA'),
'password' => env('DB_PASSWORD', '**********'),
'charset' => 'utf8',
'prefix' => '',
],
I have also change the default option in database file like below -
'default' => env('DB_CONNECTION', 'sqlsrv'),
After doing all I am facing mentioned error .
I'm developing a project in laravel and I have to change the db connection.
I add a new connection and set this like default but when I run the app it uses the old connection and I don't understand why.
I'm not using .env just setting config/database.php.
'default' => env('DB_CONNECTION', 'DB2'),
'connections' => [
'DB2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'IP'),
'database' => env('DB_DATABASE', 'DB2'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', '*****'),
'charset' => 'utf8',
'prefix' => '',
],
'DB1' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '2222'),
'database' => env('DB_DATABASE', 'DB1'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', '****'),
'charset' => 'utf8',
'prefix' => '',
],
],
How can I solve this?
Thank you
you just clear the config cache using the following commands:
php artisan config:clear
php artisan config:cache