为什么getDirty()
为空时updating
和updated
模型事件被忽略?
如果模型没有变化,并不意味着我没有更新它的关系。
以下代码:
public function updating(Eloquent $model)
{
$history = new ContentHistory($model->getOriginal());
$history->added_on = new DateTime;
$model->history()->save($history);
}
如果getDirty()
为空,则不触发。我已将$touches = ['content']
添加到ContentHistory
模型中,但无济于事。
检查触摸双亲时间戳:
当您更新任何相关模型时,如果您想在子模型更新时更新父模型的时间戳,则只需使用protected $touches = array('modelname');
属性。例如,如果你有一个Post
模型,它有一个子模型作为Comment
,如果你想更新Post
模型(只有updated_at
时间戳字段),那么在Comment
模型中添加一个$touches
属性/数组,并在该数组中添加父模型的名称,如:
class Comment extends Eloquent {
protected $touches = array('post');
public function post()
{
return $this->belongsTo('Post');
}
}