Laravel 5 雄辩的'isDirty()'和'getChanges()之间的不连贯`

Laravel 5 雄辩的'isDirty()'和'getChanges()之间的不连贯`,laravel-5,eloquent,Laravel 5,Eloquent,我目前正在从事Laravel 5.8项目,在更新模型时注意到,即使模型没有任何更改,我也会将相同的模型保存回数据库 我避免这种情况的想法如下: $model = Model::find($id); $model->fill([ "name" => $request->name, ... ]); if($model->isDirty){ $model->save() } 问题是,即使我不更改模型中的值,我仍然输入if条件并保存模型。我尝试使用

我目前正在从事Laravel 5.8项目,在更新模型时注意到,即使模型没有任何更改,我也会将相同的模型保存回数据库

我避免这种情况的想法如下:

$model = Model::find($id);
$model->fill([
    "name" => $request->name,
    ...
]);
if($model->isDirty){
    $model->save()
}
问题是,即使我不更改模型中的值,我仍然输入if条件并保存模型。我尝试使用一个临时变量并调试$model->getChanges,结果得到一个空数组


这是预期的行为吗?

有区别是的

相关代码:

isDirty码

/**
 * Determine if the model or any of the 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()
    );
}
/**
 * Get the attributes that have been changed since last sync.
 *
 * @return array
 */
public function getDirty()
{
    $dirty = [];
    foreach ($this->getAttributes() as $key => $value) {
        if (! $this->originalIsEquivalent($key, $value)) {
            $dirty[$key] = $value;
        }
    }
    return $dirty;
}
/**
 * Get the attributes that were changed.
 *
 * @return array
 */
public function getChanges()
{
    return $this->changes;
}
getChanges&getDirty代码

/**
 * Determine if the model or any of the 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()
    );
}
/**
 * Get the attributes that have been changed since last sync.
 *
 * @return array
 */
public function getDirty()
{
    $dirty = [];
    foreach ($this->getAttributes() as $key => $value) {
        if (! $this->originalIsEquivalent($key, $value)) {
            $dirty[$key] = $value;
        }
    }
    return $dirty;
}
/**
 * Get the attributes that were changed.
 *
 * @return array
 */
public function getChanges()
{
    return $this->changes;
}
总而言之

这篇文章的答案是:

保存前使用isDirty和getDirty查看哪些属性 在从数据库检索时发生了更改 以及调用的时间,同时使用wasChanged和getChanges 保存后,查看属性是否在上一次更改/更新 从代码保存到数据库


你参加isDirty检查的原因是,在检查之前,你要填写。我想这会自动填充你的。所以,事实上,在这种情况下,模型已经改变了。

有一个区别是的

相关代码:

isDirty码

/**
 * Determine if the model or any of the 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()
    );
}
/**
 * Get the attributes that have been changed since last sync.
 *
 * @return array
 */
public function getDirty()
{
    $dirty = [];
    foreach ($this->getAttributes() as $key => $value) {
        if (! $this->originalIsEquivalent($key, $value)) {
            $dirty[$key] = $value;
        }
    }
    return $dirty;
}
/**
 * Get the attributes that were changed.
 *
 * @return array
 */
public function getChanges()
{
    return $this->changes;
}
getChanges&getDirty代码

/**
 * Determine if the model or any of the 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()
    );
}
/**
 * Get the attributes that have been changed since last sync.
 *
 * @return array
 */
public function getDirty()
{
    $dirty = [];
    foreach ($this->getAttributes() as $key => $value) {
        if (! $this->originalIsEquivalent($key, $value)) {
            $dirty[$key] = $value;
        }
    }
    return $dirty;
}
/**
 * Get the attributes that were changed.
 *
 * @return array
 */
public function getChanges()
{
    return $this->changes;
}
总而言之

这篇文章的答案是:

保存前使用isDirty和getDirty查看哪些属性 在从数据库检索时发生了更改 以及调用的时间,同时使用wasChanged和getChanges 保存后,查看属性是否在上一次更改/更新 从代码保存到数据库

你参加isDirty检查的原因是,在检查之前,你要填写。我想这会自动填充你的。因此,事实上,在这种情况下,模型已经改变了