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

[Laravel 7] json_decode error when updating a json-field #680

Closed
kapersoft opened this issue Mar 4, 2020 · 12 comments · Fixed by #688
Closed

[Laravel 7] json_decode error when updating a json-field #680

kapersoft opened this issue Mar 4, 2020 · 12 comments · Fixed by #688

Comments

@kapersoft
Copy link
Contributor

Hi folks,

In my project I am using this awesome package to log changes in my models. Today I started the upgrade from Laravel 6 to Laravel 7 and stumbled onto a problem when logging an json field.

In my model I have a json field with unstructured data. In the model the json is field is casted to array using protected $casts = ['json_field' => 'array'];. I have added to field to the activity log using protected static $logAttributes = ['json_field'];. When I update this field the following error pops up:

json_decode() expects parameter 1 to be string, array given

See https://flareapp.io/share/VmeYkqmQ for the stacktrace. Removing the LogsActivity-trait or migration back to Laravel 6 will solve this error, but that's obviously not an option.

I have made a small example repo with the problem at https://github.com/kapersoft/laravel-activity-json-error. Follow the steps in the readme to seed your database and create a webserver. If you hit the route http://127.0.0.1:8000/activity-error, the error will pop up.

Does anyone has an idea why this error appears? Is there something wrong in the package or do I have to change some things to get this working again?

@developer-ssr
Copy link

This happened to me too when using attribute casting. I have a field casted to object and it produces the same error. What I did is I created a custom casts (https://laravel.com/docs/7.x/eloquent-mutators#attribute-casting) and it worked.

@Gummibeer
Copy link
Collaborator

Hey,
strange behavior and I will see what we can do. My first idea is that The default casts don't keep track if the attribute is casted or not and it tries to re-cast the already casted attribute.
I hope that there's a different solution than replacing all casts with a custom one.

@msonowal
Copy link

msonowal commented Mar 5, 2020

@kapersoft

yes my entire cms broke

@developer-ssr
Copy link

@kapersoft I guess this is bug for laravel ... i had to change all my casts with the custom one because my entire site became unusable.

@msonowal
Copy link

msonowal commented Mar 5, 2020

@developer-ssr
I can confirm that this is a issue from the activity log as it has certain function which checks for the casting and which are not entertaining the casts already applied

@kapersoft
Copy link
Contributor Author

I've created a PR for this issue: #681

@ron-florax
Copy link

Any update on this issue please? Seems we've reached a bit of a stalemate ;)

@Gummibeer
Copy link
Collaborator

Hey all,
after reading through the docs, code and so on I've not found any easy way.
I will try to get some time for it today and find a solution. I will inform you at end of the day. It could be that we will have to come up with an ugly hot-fix and do a clean L7 only release after it.
If possible I will try to copy and author the open PR(s) but it will result in my own one.
I hope @kapersoft is fine with it.

@ron-florax
Copy link

Brilliant, thanks everyone for their efforts!

@SudoGetBeer
Copy link
Contributor

@Gummibeer
I have played around in a PR too. Just saw you made one too. Mine is a little different. If you don't like my take you can close it (or I'll do it if you merge your PR). Thanks a lot ;)

@kapersoft
Copy link
Contributor Author

@Gummibeer it's fine by me!

@Gummibeer
Copy link
Collaborator

released: https://github.com/spatie/laravel-activitylog/releases/tag/3.11.2
Thanks for the help! :)

renaforsberg824 pushed a commit to renaforsberg824/activity-log-laravel-developer that referenced this issue Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment