I have been building a web application that tracks statistics.
These statistics can range between different companies.
I decided to use a main database to house all of the l
After OP's request, I submit a second answer as a different approach.
If this is only intented for database connection details, you could use the following approach with multiple database connections in a single Laravel application.
In app/config/database.php
you define two different connections. One for your main database where your users
table is stored and one dynamic for the per-client instance:
'default' => 'mysql_main',
'connections' => [
'mysql_main' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_company' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => Auth::user()->club->db_name,
'username' => Auth::user()->club->db_user,
'password' => Auth::user()->club->db_pass,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
],
As you can see you dont actually need to read the connection details of the second connection from the config file (it would still be used to get host, port and any common details with your main database), you can pass the different connection details dynamically from your logged-in user. However this will require you to have created all the databases for every user in advance.
After you have managed to connect to both databases, you will have to select the active connection anytime you execute a query.
Hopefully Schema Builder, Query Builder and Eloquent support this out of the box:
Shema Builder
Schema::connection('mysql_company')->create('statisticA', function($table)
{
$table->increments('id'):
});
Query Builder
$companyStatA = DB::connection('mysql_company')->select(...);
Eloquent Model
class StatisticA extends Eloquent {
protected $connection = 'mysql_company';
}
Controller
class SomeController extends BaseController {
public function someMethod()
{
$statA = new StatisticA;
$statA->setConnection('mysql_company');
$something = $statA->find(1);
return $something;
}
}
However what would be tricky is to define relationships between models stored in different databases. It is be possible but it strongly depends on your database driver and settings.
I doubt it would function correctly between different types of databases (eg MySQL as your main database and PostgreSQL for you company databases), so I'd suggest you to stick with MySQL and see how this goes.