📜  laravel 中的触摸 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:50.727000             🧑  作者: Mango

代码示例1
/*
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();
}