Laravel MorphToMany не работает для нескольких столбцов - PullRequest
2 голосов
/ 09 июля 2020

Laravel версия: 7.0 Вот моя таблица.

    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();
    });

Вот моя Service модель.

    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 объект, emails и forms имеют нулевой объект.

Кто-нибудь может мне помочь?

1 Ответ

1 голос
/ 09 июля 2020

Из вашего вопроса и комментариев:

Есть Форма, Электронная почта и Сервис. Формы могут быть связаны с любым количеством различных типов моделей. Электронные письма могут быть связаны с любым количеством различных типов моделей. Служба может иметь много форм, а служба может иметь много электронных писем.

Используя это в качестве основы, это будет наша схема:

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');
    }
}
...