Laravel 5 isDirty() 总是返回 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36329850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 isDirty() always returns false
提问by Вилислав Венков
I want to check if the model has been changed with isDirty method, but always returns false.
我想检查模型是否已使用 isDirty 方法更改,但始终返回 false。
This is my code :
这是我的代码:
if (!is_null($partnersData)) {
foreach ($partnersData as $partnerData) {
$partner = Partner::find($partnerData['partner_id']);
$partner->update($partnerData);
if($partner->isDirty()){
dd('true');
}
}
}
回答by noodles_ftw
$model->update()
updates and saves the model. Therefore, $model->isDirty()
equals false as the model has not been changed since the last executed query (which queries the database to save the model).
$model->update()
更新并保存模型。因此,$model->isDirty()
等于 false 因为自上次执行查询(查询数据库以保存模型)以来模型没有更改。
Try updating the model like this:
尝试像这样更新模型:
$partner = Partner::find($id);
foreach ($partnerData as $column => $value) {
if ($column === 'id') continue;
$partner->$column = $value;
}
if ($partner->isDirty()) {
// should be dirty now
}
$partner->save(); // $partner will be not-dirty from here