1

I am building a model and using chelout/laravel-relationship-events to capture events.

I have a model that looks like this:

    class Taxonomyterm extends Model
    {
    use HasMorphToManyEvents, HasMorphedByManyEvents {
        HasMorphedByManyEvents::newMorphToMany insteadof HasMorphToManyEvents;
    }
    ...
    public function images()
    {
        return $this->morphToMany(Image::class, 'imageable')->withTimestamps();
    }

    public function items()
    {
        return $this->morphedByMany(Item::class, 'taxonomytermable')->orderBy('taxonomytermables.id')->withPivot('id')->withTimestamps();
    }
    ...    
    protected static function boot()
    {
        parent::boot();
        static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) {
            if( $relation == 'images') {
                dd('Attached MorphToMany');
            }
        }

        static::morphedByManyAttached(function ($relation, $parent, $ids, $attributes) {
            if( $relation == 'items') {
                dd('Attached MorphedByMany');
            }
        }
    }

I was pointed to PHP docs by the maintainer of the package, and can't seem to find the right way to make this work.

I also assume that newMorphToMany is the method that conflicts between the two traits.

I am essentially lost on what to do from here to get both HasMorphToManyEvents and HasMorphedByManyEvents working on the same model. Package this is from

Toby Joiner
  • 4,306
  • 7
  • 32
  • 47
  • Try like this, and if work i will give you more info.. `use HasMorphToManyEvents, HasMorphedByManyEvents { HasMorphedByManyEvents::newMorphToMany as public HasMorphToManyEvents; }` – maki10 Aug 25 '20 at 14:22
  • That gives me: Trait method newMorphToMany has not been applied, because there are collisions with other trait methods on App\\Taxonomyterm – Toby Joiner Aug 28 '20 at 13:09
  • Actually, I don't see any error on your code. What's the execution error with your current code ? – Octet Aug 30 '20 at 23:16
  • Any error message ? Any github repo to try it locally ? – abenevaut Aug 31 '20 at 09:09
  • HasMorphedByMany and HasMorphToMany both use the newMorphToMany method and conflict with each other, so you can't use both on one model. – Toby Joiner Aug 31 '20 at 15:14

1 Answers1

0

You need to be sure of the conflicting method among the two traits. Assuming that newMorphToMany is the conflicting method:

You need to rename one of them as follows if you need them both available.

class Taxonomyterm extends Model
{
    use HasMorphToManyEvents, HasMorphedByManyEvents {
        HasMorphedByManyEvents::newMorphToMany insteadof HasMorphToManyEvents;
        HasMorphToManyEvents::newMorphToMany as newMorphToMany2;
    }
    // ... more code
}

Now, wherever you need to use HasMorphToManyEvents::newMorphToMany within the Taxonomyterm class you call it using it's new name, newMorphToMany2. Otherwise, newMorphToMany will always refer to HasMorphedByManyEvents::newMorphToMany within this class. Just to emphasize, you need to find out and be sure of the conflicting method in the two traits. I hope this leads you towards a solution...

rayalois22
  • 161
  • 3
  • 7