我有一个简单的问题,每当我使用Eloquent编辑数据库中的记录时,我都希望能够在保存之前和之后记录此行的值。
到目前为止我的解决方案:
但是到那时,调用了save方法,eloquent构建器对象有了新值,旧的值已被覆盖。
关于如何做到这一点的任何想法?
答案 0 :(得分:1)
您可以使用模型事件,有许多事件,例如saving
,updating
,deleting
...有关详细信息look here
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;
}
);
}
}