我有一个简单的问题,每当我使用Eloquent在数据库中编辑记录时,我都希望能够在保存之前和之后记录该行的值。
到目前为止,我的解决方案:
但是到那时,调用了save方法,雄辩的生成器对象有了新值,旧值已被覆盖。
有关如何执行此操作的任何想法?
您可以使用此模型的事件,有很多活动,如saving
,updating
,deleting
...更多详情请看这里
class SomeModel extends Eloquent {
protected static function boot()
{
parent::boot(); // don't forget to call the parent boot method
//On saving
static::saving(
function($record)
{
$dirty = $record->getDirty();
foreach ($dirty as $field => $newdata)
{
$olddata = $record->getOriginal($field);
if ($olddata != $newdata)
{
// Do what it takes here :)
}
}
return true;
}
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句