Laravel - Relationships - Touching Parent Timestamps
When a model defines a belongsTo
or belongsToMany
relationship to another model, such as a Comment
which belongs to a Post
, it is sometimes helpful to update the parent's timestamp when the child model is updated.
For example, when a Comment
model is updated, you may want to automatically "touch" the updated_at
timestamp of the owning Post
so that it is set to the current date and time. To accomplish this, you may add a touches
property to your child model containing the names of the relationships that should have their updated_at
timestamps updated when the child model is updated:
belongsTo(Post::class);
}
}
Parent model timestamps will only be updated if the child model is updated using Eloquent's save
method.