@mijalchev Creation of each database is something fairly easy to build out... just look into having a controller do this when you create each tenant.
As far as multi-tenancy / multi-database... I came up with a really clean / simple solution using a custom config file.
In my example I am using states as the subdomains... so like https://iowa.example.com
My approach was to create a file called /config/connection.php...
<?php
$domain = $_SERVER['HTTP_HOST'];
$domain_parts = explode('.', $domain);
$subdomain = $domain_parts[0];
if ($subdomain == 'iowa') {
config()->set('database.default', 'iowa');
}
if ($subdomain == 'nebraska') {
config()->set('database.default', 'nebraska');
}
return ['subdomain' => $subdomain];
Setup multiple databases in my env...
# Database: Nebraska
NEBRASKA_DB_CONNECTION=mysql
NEBRASKA_DB_HOST=127.0.0.1
NEBRASKA_DB_PORT=3306
NEBRASKA_DB_DATABASE=nebraska_db
NEBRASKA_DB_USERNAME=root
NEBRASKA_DB_PASSWORD=
# Database: Iowa
IOWA_DB_CONNECTION=mysql
IOWA_DB_HOST=127.0.0.1
IOWA_DB_PORT=3306
IOWA_DB_DATABASE=iowa_db
IOWA_DB_USERNAME=root
IOWA_DB_PASSWORD=
Then specify the databases in /config/database.php...
'iowa' => [
'driver' => 'mysql',
'url' => env('IOWA_DATABASE_URL'),
'host' => env('IOWA_DB_HOST', '127.0.0.1'),
'port' => env('IOWA_DB_PORT', '3306'),
'database' => env('IOWA_DB_DATABASE', 'forge'),
'username' => env('IOWA_DB_USERNAME', 'forge'),
'password' => env('IOWA_DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'nebraska' => [
'driver' => 'mysql',
'url' => env('NEBRASKA_DATABASE_URL'),
'host' => env('NEBRASKA_DB_HOST', '127.0.0.1'),
'port' => env('NEBRASKA_DB_PORT', '3306'),
'database' => env('NEBRASKA_DB_DATABASE', 'forge'),
'username' => env('NEBRASKA_DB_USERNAME', 'forge'),
'password' => env('NEBRASKA_DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Now my databases are set manually in the .env and /config/database.php but you could simply store the database config to a table, and just pop new users in there when they are created in the app... then in your /config/database.php make a data call above the return, store all the configs to an array, then pass that into the config dynamically.
So in /config/database.php...
<?php
use Illuminate\Support\Str;
$multitenant_dbs = DB::table('tenant_db_configs');
return [
........
'connections' => [
$multitenant_dbs
]
Something to that effect. This leaves a bit of homework for you... but as far as multi-tenant/separate db, I really was happy with this implementation. You don't have to do anything fancy with subdomains in routes or anything... it just detects the subdomain and switches the database accordingly. Also, since you're returning the subdomain in a config... you have access to that everywhere in the app.
config('connection.subdomain')