1
我爲許多租戶構建應用程序,每個租戶都有自己的數據庫。數據庫的名稱與租戶ID相同(該ID存在五個數字中)。認證後,用戶應該連接到他自己的數據庫並重定向到他的儀表板。Orchestral/tenanti多個數據庫
我試圖給用戶,他與下面的代碼,我放在Authenticate.php數據庫連接
if (!Auth::guest() && Auth::user()->tenant) {
$user = Auth::id();
Tenanti::driver('user')->asDefaultDatabase($user, 'users_{id}');
Config::set('database.connections.mysql.database', 'user_'.$user);
}
中如果主數據庫語句檢查登錄的用戶是一個租戶(布爾)。
配置/ database.php中包含以下代碼:
'tenants' => [
'user_1' => [
'driver' => 'mysql',
'host' => 'localhost', // for user with id=1
'database' => '86097', // for user with id=1
'username' => 'root', // for user with id=1
'password' => 'root', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
AppServiceProvider:
<?php namespace App\Providers;
use Orchestra\Support\Facades\Tenanti;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Tenanti::setupMultiDatabase('tenants', function (User $entity, array $template) {
$template['database'] = "user_{$entity->getKey()}";
return $template;
});
}
}
?>
我沒有得到一個錯誤,但連接沒有改變。用戶數據庫爲空,因此當我使用user_id = 1登錄時,我不應該看到任何數據。預先感謝您的幫助。
我改變了上述事情,但數據仍然顯示,但租戶的數據庫是完全空的。除此之外,如果只有用戶使用id = 1的配置時,如何添加第二個和第三個用戶? – Novy
閱讀https://github.com/orchestral/tenanti#setup-model-observer 說實話,最好先理解在單個數據庫上構建多租戶的概念,然後再跳過許多事情並假設一鍵安裝將解決一切。 一些代碼示例: https://github.com/crynobone/todoist/compare/master...single-database https://github.com/crynobone/todoist/compare/single-database .. 。多數據庫 p/s:這是不完整的,因爲我從來沒有設計它來解決所有問題。 – crynobone
我已成功實施多租戶單一數據庫。感謝您的幫助示例。應用程序是否需要進行大量修改才能使其成爲多數據庫系統?每個用戶至少有四個大表(每個用戶超過一百萬行)因此,我認爲最好的解決方案是擁有一個多數據庫系統。糾正我,如果我錯了。 – Novy