laravel-relationship-events icon indicating copy to clipboard operation
laravel-relationship-events copied to clipboard

Observer for ManyToMany

Open flatcapco opened this issue 3 years ago • 0 comments

Hands up this will totally be my issue but I can't quite work out why. (new to Laravel).

I've setup both traits on my BookingFamilyMember Model:

    use HasBelongsToManyEvents;
    use HasRelationshipObservables;

And set the boot on the model as described

        static::belongsToManyAttaching(function ($relation, $parent, $ids) {
            Log::debug("Attaching  to member {$parent->name}. ");
        });

And in my BookingFamilyMemberObserver I have this:

    public function belongsToManyAttaching($relation,BookingFamilyMember $member,$ids)
    {
        dump($relation);
    }

Now in your example you show $relation being tied to Model.. however if I do that it fails.. My relation return "journeys" Which is how it's being attached:

$member->journeys()->attach(1);

Is that all good - or am I missing something by loosely attaching it like this for a ManytoMany? It works.. I just didn't want to miss something obvious if it should be tied to a Model.

Thank you!

flatcapco avatar Oct 06 '20 16:10 flatcapco