更新和更新的模型事件将被忽略


updating and updated model events are ignored

为什么getDirty()为空时updatingupdated模型事件被忽略?

如果模型没有变化,并不意味着我没有更新它的关系。

以下代码:

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');
    }
}