繁体   English   中英

Laravel - Spatie 多租户 - 让表遵守租户数据库

[英]Laravel - Spatie Multi-tenancy - Getting tables to adhere to tenant database

我正在使用 Spatie 的多租户 package 在我的应用程序上实现多租户。 我正在使用多数据库方法,目前我不确定 my.env 文件中的 go 应该是什么,所以我有 DB_DATABASE=landlord 指向我的房东数据库。 然后我使用 DomainTenantFinder,它工作得很好。 我确实有一个问题,通常当我想指示 model 应该使用租户数据库连接时,我在 model 中包含以下内容:

use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;

class MyModel extends Model
{
    use UsesTenantConnection;

但是..我遇到的问题是,当使用 pivot 表(如下表)和 DB class 时(在我声明模型表的同一迁移中声明)

Schema::create('mymodel_othermodel', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('mymodel_id');
        $table->unsignedBigInteger('someother_id');

        $table->foreign('mymodel_id')
            ->references('id')
            ->on('my_models');

        $table->foreign('someother_id')
            ->references('id')
            ->on('some_other_models');
    });

我写信如下:

DB::table('mymodel_somemodel')->insert([
        'mymodel_id' => $mymodel->id,
        'someother_id' => $someother->id,
    ]);

Laravel 现在尝试在房东数据库而不是租户数据库上查找mymodel_othermodel表(即使我使用的是正确的域)。 使用 DB class 时,如何让 pivot 表尊重租户数据库?

另外奖励:使用多租户时,数据库设置下的 my.env 文件中的 go 应该是什么? ;)

使用 DB Facade,您应该像这样指定连接名称:

DB::connection('tenant')->table('mymodel_othermodel')->....

暂无
暂无

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

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