讨论数量:
抛块砖,我用的是laravel 6, 5.x 看下支不支持,file_put_contents是我自己乱写,可以改成接入日志服务(但是Model里搞这些事感觉怪怪的)
class WordModel extends Model {
public static function boot() {
parent::boot();
$attributeLabels = [
'id' => '主键',
'word' => '单词',
'speech' => '词性'
];
self::saved(function(self $model) use ($attributeLabels){
foreach ($attributeLabels as $attribute => $label) {
if ($model->isDirty($attribute)) { // 判断是否修改
$old = $model->getOriginal($attribute); // 原始值
$new = $model->$attribute; // 新值
file_put_contents('test.txt',"{$label} 由 {$old} 改为 {$new};\r\n", FILE_APPEND);
}
}
});
self::deleted(function (){});
self::created(function (){});
//...查源码还支持什么事件
}
}
推荐文章: