简体   繁体   English

雄辩的模型观察者可以访问受事件影响的模型数据吗?

[英]Can Eloquent model observer access model data that are being affected by the event?

I am trying to create an observer for an Eloquent model to work as a logger for the changes on that model. 我正在尝试为Eloquent模型创建observer ,以充当该模型更改的记录器。 I am not sure what parameters are passed to the observer's methods from the model. 我不确定从模型将哪些参数传递给观察者的方法。 If the parameters don't include an instance of the updated model, is there another way around it without having to redefine the model class and override the needed methods? 如果参数不包含更新后的模型的实例,是否有另一种解决方法,而不必重新定义模型类并覆盖所需的方法?

class UserObserver{
    public static function saved($user){
        Activity::create([
            "activity-name" => "save",
            "user" => $user->id
        ]);
    }    

}

I found out that the model is actually passed, my mistake was not adding user property to the fillable array in the Activity model. 我发现该模型实际上已经通过,我的错误是没有将user属性添加到Activity模型的fillable数组中。

usually, I get an exception when my application tries to update fields that are not included in the fillable array, but this time I didn't. 通常,当我的应用程序尝试更新fillable数组中未包含的字段时,会出现异常,但是这次却没有。 anybody knows why? 有人知道为什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM