Laravel - Set up dynamic database connection on Multi tenant application - database

Within my app I have different businesses and those have a number of users.
For example:
Business A has UserA, UserB and UserC
Business B has UserD and UserE
And so on..
Each business has its own separate database, so Users A, B and C access the same database, and Users D and E access a different database (Every tenant database is identical in structure, the only thing that differs is the data).
There is a main database that has this information for each user, so I know which database a user belongs.
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'main_database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => DYNAMIC_DATABASE_NAME_GOES_HERE,
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
I need to find a way to do the following in Laravel:
User signs in to the app
After login I get the user database identifier/database name using the main database connection
Set that specific database name within a connection called "tenant"
App uses that tenant connection to load the data of that specific
user/business
How can I accomplish this in Laravel 5.4?

This link has a very good example of what you are looking for.
1) Setup two connections in your database config.
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'prefix' => '',
)
2) Then to switch the DB, put the following code in your filters or middleware. Considering you have Tenant model for database connection info.
$tenant = Tenant::whereSubDomain($subdomain)->first();
Config::set('database.connections.tenant.host', $tenant ->host);
Config::set('database.connections.tenant.username', $tenant ->username);
Config::set('database.connections.tenant.password', $tenant ->password);
Config::set('database.connections.tenant.database', $tenant ->database);
//If you want to use query builder without having to specify the connection
Config::set('database.default', 'tenant');
\DB::purge('tenant');
\DB::reconnect('tenant');
dd(\DB::connection('tenant'));
3) Specify following in your models
//On models dealing with the main DB
protected $connection = 'main';
//On models dealing with tenant DBs
protected $connection = 'tenant';

Related

How to access remote database from laravel 4.2

I am currently do some development in a early developed system developed from laravel 4.2.
I run it in my local machine. At the same time now I have to access to a another System's database.
For testing purposes i have created that db in my local server. I don't have direct access to that db in real situation. Only i have a view. So please tell me how should i configure my system to achieve that task.
First you have to add a separate database connection to your other system's database, so in your app/config/database.php you can have this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge2',
'username' => 'forge2',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In order to perform query to the other database you have to do this:
$users = DB::connection('mysql2')->select('select * from users where id = ?', array(1));
You may see this code in the Laravel's documentation: https://laravel.com/docs/4.2/database#accessing-connections

DRUPAL 8 - module Views database connector

I need for my drupal 8 installation to connect a second database for a view.
I install the module views_database_connector (january 2015 release)
I connect with the setting.
but nothing works, i don't see my database with view.
my settings...
$databases['default']['default'] = array (
'database' => 'first base',
'username' => 'first base',
'password' => 'xxxxx',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
$databases['external']['default'] = array (
'database' => 'second_base',
'username' => 'second_base',
'password' => 'yyyyyy',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
Does this module need a patch, and how to apply ??
For this you can create multiple database connection in setting file :
use \Drupal\Core\Database\Database::setActiveConnection('external'); to get current active connection.

Laravel change database connection [duplicate]

This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Laravel Change Connection Dynamically
(2 answers)
Closed 5 years ago.
How can a database connection be changed when user is admin role? Admin database contains information that must not be accessible to other user's and requirement also is that database must be located on own server. I do have currently separate configuration for admin connection.
'mysql' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'secretpassword',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I want that no hardcoding is in application so how can Laravel do the switch when necessary? Thanks for any help.
You need to create new database config rule in appconfig.
'mysql' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'secretpassword',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
'mysql_admin' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'admindatabase',
'username' => 'root',
'password' => 'secretpassword1',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I named mysql_admin. In migration when use
Schema::create('some_table', function($table)
to create table ,use
Schema::connection('mysql_admin')->create('some_table', function($table)
to create that table in other database.
For query to that database do something like this
$admins = DB::connection('mysql_admin')->select(...);
And you can to define connection for model
class AdminModel extends Eloquent {
protected $connection = 'mysql_admin';
}

how to connect to multiple databases in drupal in order to display the content in a table

i am using drupal in order to create a website on my localhost machine.
i have a mysql database that i want to connect it to the drupal website and display its data in a table.
this is what i did in settings.php file
//connect to external database
$databases['sitesdb']['default'] = array (
'database' => 'sitesdb',
'username' => 'xxxxxxx',
'password' => 'xxxxxxxx',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
and in the drupal i download the Views Database Connector (VDC) module in order to connect to the external database.
but it did not work .
second method i used views and created content types that are the fields table of the external databases.
but i do not know how to continue to display the data.
I was using migrate module and multiple DB configuration looked like:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'main_db',
'username' => 'XXX',
'password' => 'XXX',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'legacy' =>
array (
'default' =>
array (
'database' => 'additional_db',
'username' => 'XXX',
'password' => 'XXX',
'host' => 'XXX',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);

Variable database setting Cakephp

I'm searching a way to create a variable database in Cakephp.
I'm creating a website in wich the database setting (user, pass, db) depends of the user who logg's in. The database settings come from another database.
Thanks in advance,
AƤron
-- ANSWER
Thanks for your quick reply.
I know how to switch between databases, but the database settings have to be variable.
Example
$db = $this->DB_SET['DB_SET']['db']; //from model DB_SET the database
$db_login= $this->DB_SET['DB_SET']['login'];
$db_host= $this->DB_SET['DB_SET']['host'];
$db_pass= $this->DB_SET['DB_SET']['pass'];
var $db2 = array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
);
If you're looking to create database connections on the fly, you may want to use ConnectionManager::create().
// get logged in user's id and create a connection for them
$id = $this->Auth->user('id');
ConnectionManager::create("user_$id_db", array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
));
Then set your models to use it when you're ready.
// get logged in user's id and use their connection
$id = $this->Auth->user('id');
$this->Model->setDataSource("user_$id_db");

Resources