【问题标题】:Laravel multi tenancy switching issueLaravel 多租户切换问题
【发布时间】:2019-02-27 07:46:16
【问题描述】:

我有一个情况。我有一个既有管理员又有租户的应用程序。在管理员中,我们有用户管理、租户管理等模块。在租户中,我有用户管理和其他一些模块。
AppServiceProvider 中,我已将默认连接设置为this link 中提到的租户(仅在此之后租户登录才有效)。当我通过管理员创建租户时,我将this link 中提到的连接切换到新创建的客户,并使用默认的 User 模型创建用户。此用户模型是默认模型,我没有在其中使用租户连接特征。这如预期的那样在租户数据库中创建了一个新用户。但是当我尝试在另一个控制器中切换连接时,直到我使用另一个具有租户特征的 CustomerUser 模型之前,该开关才起作用。当我使用默认的用户模型时,获取的数据来自系统。我发现的唯一区别是切换上方的代码,即,

$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;

我使用这个返回的网站 id 切换到租户。

但是当我在另一个控制器中尝试时,切换代码是相同的,但为了获得网站,我使用了这个代码:

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

常用开关代码:

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

请提出解决问题的方法。

【问题讨论】:

    标签: php laravel multi-tenant


    【解决方案1】:

    下面的sn-p可以用来配置app:

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

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 2021-07-04
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2019-04-14
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多