本篇文章帶大家聊聊laravel數據模型中isdirty()和waschanged()的區別,希望對大家有所幫助!
laravel 數據模型中 `isDirty()` 和 `wasChanged()` 有區別嗎?
答案:是有區別的。
相關代碼: github.com/laravel/framework/blob/…
isDirty 函數的代碼如下:
/** * 判斷模型或者任意指定模型屬性是否被修改過 * * @param array|string|null $attributes * @return bool */public function isDirty($attributes = null){ return $this->hasChanges( $this->getDirty(), is_array($attributes) ? $attributes : func_get_args() );}
getChanges() 和 getDirty() 函數的代碼如下
/** * 獲取自從最后一次同步以來,被修改的屬性值 * * @return array */public function getDirty(){ $dirty = []; foreach ($this->getAttributes() as $key => $value) { if (! $this->originalIsEquivalent($key, $value)) { $dirty[$key] = $value; } } return $dirty;}/** * 獲取所有已經被修改的屬性. * * @return array */public function getChanges(){ return $this->changes;}
簡而言之.
答案引用于: github.com/laravel/framework/blob/…
isDirty (and getDirty) 用在保存前置執行, 查看哪些屬性在從數據庫檢索到調用之間被修改過, 而 wasChanged (and getChanges)是保存后置執行,查看屬性是否在上次保存中(從代碼到數據庫)被修改或者更新.
原文地址:https://stackoverflow.com/questions/58312036/incoherence-between-eloquent-isdirty-and-getchanges
譯文地址:https://learnku.com/laravel/t/61576
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END