Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cast to array bug #681

Closed
wants to merge 2 commits into from
Closed

Fix cast to array bug #681

wants to merge 2 commits into from

Conversation

kapersoft
Copy link
Contributor

Fixes #680; test for the issue is included.

Let me know if you have any questions or remarks.

Copy link
Collaborator

@Gummibeer Gummibeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems good to me - will check it in depth tomorrow

@@ -38,6 +38,7 @@
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^5.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still support L6 - so we also need Orchestra 4.

@@ -133,8 +133,9 @@ public static function logChanges(Model $model): array
static::getModelAttributeJsonValue($model, $attribute)
);
} else {
$changes[$attribute] = $model->getAttribute($attribute);

$changes[$attribute] = $model->hasCast($attribute, 'array') && is_array($model->attributes[$attribute])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json cast is the same. And does this happen with collections/objects too? 🤔

@Gummibeer
Copy link
Collaborator

At the end I'm still not sure what we are doing wrong? 🤔
Isn't this something to get fixed in the core? I mean in theory this should also happen if you call getAttribute() your own?

@ron-florax
Copy link

If this helps, I initially thought it was an issue in the framework, but it doesn't seem to be: laravel/framework#31790

@canvural
Copy link

canvural commented Mar 7, 2020

We also had this issue in our projects. I did some digging. I think I found something. I'm not sure.

Here it's calling getOriginal method. getOriginal method returns all the attributes, but also casts them.
So later when logChanges is working, when it calls $model->getAttribute($attribute); attribute value is already casted to array, so it throws the error.

I'm not sure this is the cause, but maybe you can investigate more.

@SudoGetBeer
Copy link
Contributor

@canvural Interesting.

Maybe it is this: https://laravel.com/docs/7.x/upgrade#factory-types

The $model->getOriginal() method will now respect any casts defined on the model. Previously, this method returned the uncast, raw attributes. If you would like to continue retrieving the raw, uncast values, you may use the getRawOriginal method instead.

@canvural
Copy link

canvural commented Mar 7, 2020

Yeah, that looks like the reason.

So, maybe this issue can be solved by using getRawOriginal (Edit: This method is added in Laravel 7. So it won't work in earlier versions)

@Gummibeer
Copy link
Collaborator

Thanks @kapersoft for your work here! 🎉 I was able to use everything and have extended your PR/branch.
I will close this PR in favor of #688 .

@Gummibeer
Copy link
Collaborator

released: https://github.com/spatie/laravel-activitylog/releases/tag/3.11.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Laravel 7] json_decode error when updating a json-field
6 participants