mercredi 30 novembre 2016

How to automatically update the timespamps of parent model when a relationship is being created or removed?

In Laravel a model that has a belongs to relationship to another, also has a <parent>_id field on its table that points to the id of the parent. When that model is being associated to another the updated_at timespamp naturally gets updated, as the value of the <parent>_id field changes.

The parent model though, the one with the has many relationship, doesn’t have any fields that change when it becomes or stops being related to a child model, so its updated_at timespamp doesn’t change.

What I want, is both the parent and the child models to automatically update their updated_at timespamps every time a relationship between the two gets created or removed. What is the proper way to achieve this?

I have already looked into the touches property, which causes the timestamp of the parent to get updated automatically when the child gets modified. But this only works when a new relationship is being created and not when an old one is being removed. And also, this will update the timestamp of the parent at the change of any field, not just of the <parent>_id and that's something that I don't actually want.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2fCCLwF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire