简体   繁体   English

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

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

I'm using Spatie's mutlti-tenancy package to implement multi-tenancy on my app.我正在使用 Spatie 的多租户 package 在我的应用程序上实现多租户。 I'm using the multiple database approach, and at the moment I'm unsure what should go in my.env file, so I've got DB_DATABASE=landlord in there to point to my landlord database.我正在使用多数据库方法,目前我不确定 my.env 文件中的 go 应该是什么,所以我有 DB_DATABASE=landlord 指向我的房东数据库。 I then use the DomainTenantFinder and it works quite well.然后我使用 DomainTenantFinder,它工作得很好。 I do have an issue though, usually when I want to indicate a model should use the tenant database connection, I include the following in the model:我确实有一个问题,通常当我想指示 model 应该使用租户数据库连接时,我在 model 中包含以下内容:

use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;

class MyModel extends Model
{
    use UsesTenantConnection;

But.. I have the issue that when using pivot tables like the one below with the DB class (declared in the same migration where I declare my model's table)但是..我遇到的问题是,当使用 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');
    });

I write to this as follows:我写信如下:

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

Laravel now tries to look for the mymodel_othermodel table on the landlord database instead of on the tenant databse (even though I'm using the right domain). Laravel 现在尝试在房东数据库而不是租户数据库上查找mymodel_othermodel表(即使我使用的是正确的域)。 How do I get pivot tables to respect the tenant database when using the DB class??使用 DB class 时,如何让 pivot 表尊重租户数据库?

Also bonus: What should go in my.env file under DB settings when using multi-tenancy?另外奖励:使用多租户时,数据库设置下的 my.env 文件中的 go 应该是什么? ;) ;)

using the DB Facade you should specify the connection name like so:使用 DB Facade,您应该像这样指定连接名称:

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

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

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