laravel eloquent touches code example
Example: touches in laravel
/*
When a model has a BelongsTo or BelongsToMany relationship with another model,
Let us say Comment that belongs to a Blog, it could in some cases be helpful
to update the parent’s timestamp when the child is updated.
This can be done by adding the relationship to the $touches attribute.
*/
class Comment extends Model
{
protected $touches = ['blog'];
public function blog()
{
return $this->belongsTo(App\Blog::class);
}
}
/*
In Case we only want to Update One Model without needing change in other Model
ie Update updated_at without updating other fields
See the code here:
*/
public function updateUpdatedAt(){
$user = User::find(1);
$user->touch();
}