Laravel-activitylog: [Laravel 7] json_decode error when updating a json-field

Created on 5 Mar 2020  路  12Comments  路  Source: spatie/laravel-activitylog

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?

bug help wanted question

Most helpful comment

@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 ;)

All 12 comments

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.

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.

@kapersoft

yes my entire cms broke

@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.

@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

I've created a PR for this issue: https://github.com/spatie/laravel-activitylog/pull/681

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

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.

Brilliant, thanks everyone for their efforts!

@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 ;)

@Gummibeer it's fine by me!

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

Was this page helpful?
0 / 5 - 0 ratings