3

Laravel version: 7.0 Here is my 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();
});

Here is my Service model.

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

I inserted data in model_email_form table but when I get service model object, emails and forms have null object.

Can anyone help me?

Roman
  • 2,530
  • 2
  • 27
  • 50
LoveCoding
  • 1,121
  • 2
  • 12
  • 33
  • MorphToMany is for polymorphic many-to-many. The model_email_form table you have provided is not correct for this. Also, please describe in more detail the raltionships between Form, Email and Service. Are there many Forms that relate to more models than Service? Likewise, are there many Emails that relate to more models than Service? Is there a reason you want both polymorphic relations to be stored on the same table? – Kurt Friars Jul 09 '20 at 15:26
  • Thanks for your reply. The relationships between Form, Email and Service are all many-to-many, and the reason why I put form_id and email_id together there is that service requires the both. – LoveCoding Jul 09 '20 at 15:40
  • It can require both, and not have them in the same table. I will make an answer on my interpretation of what is suitable and if it is not suitable, I will delete it – Kurt Friars Jul 09 '20 at 15:41
  • I just separated them into two tables. But it still doesn't work. – LoveCoding Jul 09 '20 at 15:46
  • Ahh, I just found the issue. My fault. Thanks for your help. – LoveCoding Jul 09 '20 at 15:48
  • Glad you solved your issue, my solution below is how I would implement these relationships. – Kurt Friars Jul 09 '20 at 15:54
  • if my answer is in line with the solution please give it an upvote or accept it. – Kurt Friars Jul 09 '20 at 16:01

1 Answers1

1

From your question and comments:

There are Form, Email and Service. Forms can be associated with any number of different types of models. Emails can be associated with any number of different types of models. A Service can have many Forms and a Service can have many Emails.

Using that as the basis, this would be our schema:

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

With that schema, we can create the following models with the following relationships:

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');
    }
}
Kurt Friars
  • 3,625
  • 2
  • 16
  • 29