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

问题描述 投票:0回答:2
描述

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

为什么要添加这个

我试图获取租户的计划和功能,并希望从中央域(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
        
    }
php mysql laravel multi-tenant
2个回答
8
投票

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

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

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

0
投票

您可以参考这个链接Central App

在database.php中设置另一个连接并使用

 DB::connection($connectionName)->table('foo')->where(...)

© www.soinside.com 2019 - 2024. All rights reserved.