简体   繁体   English

Laravel多租户切换问题

[英]Laravel multi tenancy switching issue

I have a situation. 我有情况 I have an application that has both admin and tenants. 我有一个既有管理员又有租户的应用程序。 In admin we have modules like user management, tenant management etc. In tenants I have user management and some other modules. 在管理员中,我们有用户管理,租户管理等模块。在租户中,我有用户管理和其他一些模块。
In AppServiceProvider I have set the default connection as tenant as mentioned in this link (Only after this the tenant login worked). AppServiceProvider中,如该链接中所述,我已将默认连接设置为租户(仅在此之后,租户登录有效)。 When I create a tenant via admin, I switch the connection as mentioned in this link to the newly created customer and create a user using the default User Model. 当我通过admin创建租户时,我将如本链接中所述将连接切换到新创建的客户,并使用默认User模型创建用户。 This User model is the default one and I have not used the tenant connection trait in this. 该用户模型是默认模型,在此我没有使用租户连接特征。 This created a new user in the tenant database as expected. 这将按预期在租户数据库中创建一个新用户。 But when I tried to switch the connection in another controller, the switch did not work until I used another CustomerUser model with the tenant trait. 但是,当我尝试在另一个控制器中切换连接时,直到我使用具有租户特征的另一个CustomerUser模型,该切换才起作用。 When I used the default User model, the data fetched was from the system. 当我使用默认的用户模型时,获取的数据是从系统中获取的。 The only difference I found was the code just above the switching, which is, 我发现的唯一区别是切换上方的代码是

$website = new Website;
$website->uuid = $customer->domain_name;
app(WebsiteRepository::class)->create($website);
$hostname = new Hostname;
$serverName = $request->server->get('SERVER_NAME');
$hostname->fqdn = $website->uuid.'.'.$serverName;
$hostname = app(HostnameRepository::class)->create($hostname);
app(HostnameRepository::class)->attach($hostname, $website);
return $website->id;

I use this returned website id to switch to the tenant. 我使用此返回的网站ID切换到租户。

But when I tried in the other controller the switching code was same but to get the website I used this code: 但是,当我尝试使用其他控制器时,切换代码是相同的,但是要获得网站,我使用了以下代码:

$customerWebsite = Customers::findOrFail($customerId)->website;
$uuid = $customerWebsite->uuid;
$website = app(WebsiteRepository::class)->findByUuid($uuid);

The common switch code: 常用的开关代码:

$tenancy = app(Environment::class);
$tenancy->tenant($website);

Please suggest a way to fix the issue. 请提出解决此问题的方法。

The following snippet can be used to configure the app: 以下代码段可用于配置应用程序:

app(Environment::class)->tenant($hostname->website);
config(['database.default' => 'tenant']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM