mercredi 9 octobre 2019

Incoherence between eloquent `isDirty()` and `getChanges()`

I am currently working on a Laravel 5.8 project and when updating a model noticed that even though there aren't any changes to the model I'm saving the same model back into the database.

My thinking to avoid this was the following:

$model = Model::find($id);
$model->fill([
    "name" => $request->name,
    ...
]);
if($model->isDirty){
    $model->save()
}

Problem is that even though I don't change values in my model I'm still entering the if() condition and saving the model. I tried using a temp variable and debugged $model->getChanges() and I get an empty array.

Is this expected behavior?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire