jeudi 3 décembre 2015

Calling save() on morphOne() relationship with an existing record creates a new record without removing the old one

If you have a Model, let's say a Post model, with a morphOne() relationship to an Image model. The second time you $post->image()->save() you now have two entries in the images table for the same Post. Since it's a one-to-one relationship, it seems like this shouldn't happen. $post->image continues to refer to the first one saved.

The associate() method doesn't help either. You can only call it on the Image with the morphTo property - it will update the post ID but still do nothing to affect the first Image saved.

Is this a bug? Should I file it on the Laravel Github? Is there some way around this - do I simply have to manually delete the existing one? Someone else also made a note of this 6 months ago, I am surprised there isn't a fix for this by now.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire