samedi 29 février 2020

Failed to restore eloquent model after it has been un-serialized (Laravel 5.4)

I cannot understand why an un-serialized eloquent model cannot be saved in the database table.

I am trying to restore previous versions of a "document" which are represented by an App\Document model. Before each document model is updated the previous "version" is serialized and stored in a different table so it can be retrieved in the future.

When I retrieve the serialized model and un-serialize it (unserialize()) I am calling the save() method on the model but nothing happens.

The code:

$targetModel = unserialize($serializedModel);                       
$modelSaved = $targetModel->save();

$modelSaved is always true but the row in the database table does not change.

After looking at the API i saw that when we call save() on a model then only if the model exists and is dirty will be updated.

Please note that if I 'dump' the $targetModel the properties attributes and original are the same and the call to isDirty() returns false.

I have also tried modifying an attribute of the un-serialized model so that isDirty() returns true but that has not changed anything.

Any ideas on how I can save the un-serialized model in the database? I am doing something wrong here, for sure.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire