Laravel - Spatie 多租户 - 获取表以遵守租户数据库
Laravel - Spatie Multi-tenancy - Getting tables to adhere to tenant database
我正在使用 Spatie 的多租户包在我的应用程序上实现多租户。
我正在使用多数据库方法,目前我不确定我的 .env 文件中应该放什么,所以我在里面有 DB_DATABASE=landlord 指向我的房东数据库。然后我使用 DomainTenantFinder,它运行良好。不过我确实有一个问题,通常当我想指示一个模型应该使用租户数据库连接时,我在模型中包含以下内容:
use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;
class MyModel extends Model
{
use UsesTenantConnection;
但是..我有一个问题,当使用 pivot tables 像下面的数据库 class (在我声明我的模型的 table 的同一个迁移中声明)
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 table(即使我'使用正确的域)。在使用数据库 class 时,如何让数据透视表 tables 尊重租户数据库??
还有一个好处:当使用多租户时,数据库设置下的 .env 文件应该放些什么? ;)
使用 DB Facade,您应该像这样指定连接名称:
DB::connection('tenant')->table('mymodel_othermodel')->....
我正在使用 Spatie 的多租户包在我的应用程序上实现多租户。 我正在使用多数据库方法,目前我不确定我的 .env 文件中应该放什么,所以我在里面有 DB_DATABASE=landlord 指向我的房东数据库。然后我使用 DomainTenantFinder,它运行良好。不过我确实有一个问题,通常当我想指示一个模型应该使用租户数据库连接时,我在模型中包含以下内容:
use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;
class MyModel extends Model
{
use UsesTenantConnection;
但是..我有一个问题,当使用 pivot tables 像下面的数据库 class (在我声明我的模型的 table 的同一个迁移中声明)
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 table(即使我'使用正确的域)。在使用数据库 class 时,如何让数据透视表 tables 尊重租户数据库??
还有一个好处:当使用多租户时,数据库设置下的 .env 文件应该放些什么? ;)
使用 DB Facade,您应该像这样指定连接名称:
DB::connection('tenant')->table('mymodel_othermodel')->....