Replies: 3 comments
-
Please post an example |
Beta Was this translation helpful? Give feedback.
-
Here's an example. INSERT INTO `tenants` (`id`, `name`, `domain`, `database`, `created_at`, `updated_at`) VALUES (NULL, 'foo', 'foo.laravel.test', 'foo_db_name', NULL, NULL);
INSERT INTO `tenants` (`id`, `name`, `domain`, `database`, `created_at`, `updated_at`) VALUES (NULL, 'bar', 'bar.laravel.test', 'bar_db_name', NULL, NULL); I now have 2 tenants:
Also, i have hard-coded in my landlord db each tenant's database name. In laravel database.php: 'landlord' => [
'driver' => 'mysql',
'host' => env('LANDLORD_DB_HOST', '127.0.0.1'),
'port' => env('LANDLORD_DB_PORT', '3306'),
'database' => env('LANDLORD_DB_DATABASE', ''),
'username' => env('LANDLORD_DB_USERNAME', 'forge'),
'password' => env('LANDLORD_DB_PASSWORD', ''),
],
'tenant' => [
'driver' => 'mysql',
'host' => env('TENANT_DB_HOST', '127.0.0.1'),
'port' => env('TENANT_DB_PORT', '3306'),
'database' => null,
'username' => env('TENANT_DB_USERNAME', 'forge'),
'password' => env('TENANT_DB_PASSWORD', ''),
], I see a problem here, because each tenant share the same database connection and the same database user. What i think could be more efficient is to bind the connection name instead of the database name like so: INSERT INTO `tenants` (`id`, `name`, `domain`, `database`, `created_at`, `updated_at`) VALUES (NULL, 'foo', 'foo.laravel.test', 'foo_connection', NULL, NULL);
INSERT INTO `tenants` (`id`, `name`, `domain`, `database`, `created_at`, `updated_at`) VALUES (NULL, 'bar', 'bar.laravel.test', 'bar_connection', NULL, NULL); In laravel database.php: 'landlord' => [
'driver' => 'mysql',
'host' => env('LANDLORD_DB_HOST', '127.0.0.1'),
'port' => env('LANDLORD_DB_PORT', '3306'),
'database' => env('LANDLORD_DB_DATABASE', ''),
'username' => env('LANDLORD_DB_USERNAME', 'forge'),
'password' => env('LANDLORD_DB_PASSWORD', ''),
],
'foo_connection' => [
'driver' => 'mysql',
'host' => env('TENANT_DB_HOST', '127.0.0.1'),
'port' => env('TENANT_DB_PORT', '3306'),
'database' => null,
'username' => env('TENANT_DB_USERNAME', 'forge'),
'password' => env('TENANT_DB_PASSWORD', ''),
],
'bar_connection' => [
'driver' => 'mysql',
'host' => env('TENANT_DB_HOST', '127.0.0.1'),
'port' => env('TENANT_DB_PORT', '3306'),
'database' => null,
'username' => env('TENANT_DB_USERNAME', 'forge'),
'password' => env('TENANT_DB_PASSWORD', ''),
], Hope this clarifies. |
Beta Was this translation helpful? Give feedback.
-
Is not hard to implement it by yourself. What are your doubts about it? |
Beta Was this translation helpful? Give feedback.
-
Hello there, i mean: the docs points that we need to name a tenant database after the tenant own name.
So for example tenant1 gets database tenant1, tenant2 gets database tenant2 and well, you got the idea.
My question is: wouldn't have been more general-purpose to use a tenant's name as a reference to a specific database connection? In this way we could ensure a separate connection for each tenant, but not limiting to the number of required databases.
Just asking.
Beta Was this translation helpful? Give feedback.
All reactions