gpt4 book ai didi

php - Laravel MorphToMany 不适用于多列

转载 作者:行者123 更新时间:2023-12-04 04:02:55 24 4
gpt4 key购买 nike

Laravel 版本:7.0 这是我的 table 。

Schema::create('model_email_form', function (Blueprint $table) {
$table->id();
$table->string('model_type');
$table->unsignedBigInteger('model_id');
$table->unsignedBigInteger('email_id');
$table->unsignedBigInteger('form_id');
$table->timestamps();
});

这是我的服务模型。

public function forms()
{
return $this->morphToMany(
Form::class,
'model',
'model_email_form',
'model_id',
'form_id'
);
}

public function emails()
{
return $this->morphToMany(
Email::class,
'model',
'model_email_form',
'model_id',
'email_id'
);
}

我在 model_email_form 表中插入了数据,但是当我得到 service model 对象时,emailsforms 为空对象。

谁能帮帮我?

最佳答案

来自您的问题和评论:

有表格、电子邮件和服务。表单可以与任意数量的不同类型的模型相关联。电子邮件可以与任意数量的不同类型的模型相关联。一个服务可以有很多表单,一个服务可以有很多电子邮件。

以此为基础,这将是我们的模式:

Schema::create('forms', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name'); // as an example
...
$table->timestamps();
});

Schema::create('formables', function (Blueprint $table) {
$table->unsignedBigInteger('form_id'); // the id of the form
$table->unsignedBigInteger('formable_id'); // the associated model's id
$table->string('formable_type'); // The associated model's class name
});

Schema::create('emails', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('subject'); // as an example
...
$table->timestamps();
});

Schema::create('emailables', function (Blueprint $table) {
$table->unsignedBigInteger('email_id'); // the id of the email
$table->unsignedBigInteger('emailable_id'); // the associated model's id
$table->string('emailable_type'); // The associated model's class name
});

Schema::create('services', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name'); // as an example
...
$table->timestamps();
});

使用该架构,我们可以创建具有以下关系的以下模型:

class Form extends Model
{
public function services()
{
return $this->morphedByMany(Service::class, 'formable');
}

// Add the other morphedByMany relationships of forms
}

class Email extends Model
{
public function services()
{
return $this->morphedByMany(Service::class, 'emailable');
}

// Add the other morphedByMany relationships of emails
}

class Service extends Model
{
public function forms()
{
return $this->morphedToMany(Form::class, 'formable');
}

public function emails()
{
return $this->morphedToMany(Email::class, 'emailable');
}
}

关于php - Laravel MorphToMany 不适用于多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62817893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com