LoveCoding
LoveCoding

Reputation: 1211

Laravel MorphToMany Doesn't Work for multi columns

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?

Upvotes: 3

Views: 999

Answers (1)

Kurt Friars
Kurt Friars

Reputation: 3764

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

Upvotes: 1

Related Questions