updating 和 updated 事件触发描述是否有出入?
Eloquent: 入门,观察器部分 所说,“如果模型已经存在于数据库中并且调用了 save 方法,会触发 updating 和 updated 事件。”与实际情况有点出入,或者描述有点不清晰。实际情况应该是只有调用了模型的save()方法,且模型给定属性已被修改才会触发 updating 和 updated 事件。。相关信息可以看 Eloquent\Model.php 源码 542 行。
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
// If the model already exists in the database we can just update our record // that is already in this database using the current IDs in this "where" // clause to only update this model. Otherwise, we'll just insert them. if ($this->exists) { $saved = $this->isDirty() ? $this->performUpdate($query) : true; }
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
/** * Determine if the model or given attribute(s) have been modified. * 确定模型或给定属性是否已被修改。 * * @param array|string|null $attributes * @return bool */ public function isDirty($attributes = null) { return $this->hasChanges( $this->getDirty(), is_array($attributes) ? $attributes : func_get_args() ); }
推荐文章: