0

I have a table called creative and two another tables called title_set and image_set. They relative with each other by the creative_set table.

So basically, I have:

creative:
    id
    title

title_set:
    id
    title
    
image_set:
    id
    image

And the relation table:

creative_set:
    creative_id
    set_id

Now, I create the model and the relations:

Creative Model:

public function imageSets()
{
    return $this->belongsToMany('App\Models\ImageSet', 'creative_set', 'creative_id', 'set_id');
}


public function titleSets()
{
    return $this->belongsToMany('App\Models\TitleSet', 'creative_set', 'creative_id', 'set_id');
}

public function creativeSets()
{
    return $this->hasMany('App\Models\CreativeSet', 'creative_id');
}

I create a function to remove the relationship:

public function deleteRelations()
{
    $this->titleSets()->delete();
    $this->imageSets()->delete();
    $this->creativeSets()->delete();
}

Now I create two creative A (Title Creative 01) with one relation model (Title Set 01) and B (Image Creative 01) with one relation model (Image Set 01).

When I update the creative A, I will call deleteRelations function and recreate new items.

The issue is, it not only remove the titleSets of the creative A but remove the imageSets of the creative B also. Why do we have the results for $this->imageSets()?

Any mistakes here?

Son Le
  • 229
  • 5
  • 16

0 Answers0