簡體   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