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

cpt*_*i20 3 php mysql multi-tenant laravel

描述

有没有办法切换Stancl/Tenancy模块中的DB环境?由于我之前使用过一些其他租赁模块,因此当我想要从租户内的中央域访问某些资源时,我会切换环境,反之亦然。

为什么要添加这个

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

namespace App\Helpers;
use App\Models\System\Admin\Subscription;
 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
        
    }
Run Code Online (Sandbox Code Playgroud)

cpt*_*i20 12

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

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

tenancy()->central(function ($tenant) {
    return User::all();
});
Run Code Online (Sandbox Code Playgroud)