php - Laravel MorphToMany 不适用于多列

标签 php laravel eloquent polymorphism laravel-relations

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/

相关文章:

php - 我想用 mysqldump 备份表的最后记录

php - 如何正确链接 2 个容器?

php - Laravel - whereHas 在不检查其他人的情况下检查关系的最新记录

mysql - Laravel,插入多行而不使用foreach

laravel - 如何使用 Laravel 在单个页面上对两个 Eloquent 集合进行分页?

php - 在大型 mysql 数据库上按 rand() 排序

laravel - 未找到类 'App\Http\Controllers\Api\Validator'

php - 通过浏览器访问 IP 地址时,在 nginx 上创建新的 Lumen 应用程序会抛出 404

php - 在 Laravel 4 中搜索和过滤/精炼数据库结果

php - Laravel 中 pgsql 上的 Cacti