0

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:

  1. User signs in to the app
  2. After login I get the user database identifier/database name using the main database connection
  3. Set that specific database name within a connection called "tenant"
  4. App uses that tenant connection to load the data of that specific user/business

How can I accomplish this in Laravel 5.4?

1 Answer 1

1

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';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.