Define the $touches property on child models to update parent model updated_at timestamps automatically when child records change.
When caching parent model data or HTTP responses (such as caching a Post with its Comments), adding or updating a comment leaves the parent post's updated_at timestamp unchanged, resulting in stale cache hits.
Defining $touches on the child model updates parent timestamps automatically.
Configuring $touches on Child Models
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Comment extends Model
{
// List parent relationships to touch when this model is saved or deleted
protected $touches = ['post'];
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
}
How It Works
When a comment is created, updated, or deleted:
$comment = Comment::find(1);
$comment->update(['content' => 'Updated comment text.']);
// The associated Post's updated_at timestamp is automatically set to now()!
Summary
- Automatically refreshes parent
updated_attimestamps on child save and delete events. - Essential for HTTP
ETaggeneration and timestamp-based cache invalidation. - Supports multiple parent relationship names in the array (
['post', 'author']).
Related Tips
View all tips →Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.
Remove Global Eager Loading with withoutRelation()
Use withoutRelation() or unsetRelation() to remove eager loaded relationships on specific Eloquent queries or model instances.