从租户域内的中央数据库访问数据,反之亦然


描述

有没有办法在Stancl/Tenacy模块中切换DB环境?正如我之前使用的一些其他租赁模块一样,当我想从租户内部的中心域访问一些资源时,我会切换环境,反之亦然。

为什么要添加

我试图获得租户的计划和功能,并希望从中央域(DB(获得更多数据。例如,我为租户和中心用户命名了订阅和计划表,当我试图从中心域获取租户用户的订阅时,它将从租户数据库返回数据。

namespace AppHelpers;
use AppModelsSystemAdminSubscription;
public static function checkTenantPlan()
{
// Find the tenant
// get the tenantId and find that
$tenant_user_id = tenant()->user_id;
// Find the subscription of the tenant User
$subscription = Subscription::where('user_id', $tenant_user_id)
->orderBy('created_at', 'desc')
->first();
return $subscription->plan(); //returning the data from the current tenant db

}

它已在新版本中修复。https://github.com/stancl/tenancy/releases/tag/v3.3.0

$tenant->run(function ($tenant) {
return User::all();
});
tenancy()->central(function ($tenant) {
return User::all();
});

您可以参考此链接Central App

在database.php中设置另一个连接并使用DB::connection($connectionName)->table('foo')->where(...)

最新更新