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

Activity log cannot produce enum on properties (array) #1108

Closed
pemudakoding opened this issue Nov 3, 2022 · 3 comments · Fixed by #1121
Closed

Activity log cannot produce enum on properties (array) #1108

pemudakoding opened this issue Nov 3, 2022 · 3 comments · Fixed by #1121
Labels

Comments

@pemudakoding
Copy link

Describe the bug
Hi guys, so I'm gonna let you know, but I i don't know it's a bug or not but I didn't see on docs how to handle enum cast from the model. when I cast enum to model Activity log cannot produce the enum cast from laravel and return error.
but the data successfully send to my database.

To Reproduce

  1. Set cast on model, enum cast that introduce by Laravel.
  2. Setup log options in the model
  3. Try to create / update

Expected behavior
The log should able to produce enum like convert to string or etc instead of return error

Screenshots
image

Versions (please complete the following information)

  • PHP: 8.1
  • Database: MYSQL
  • Laravel: 9.83.0
  • Package: spatie/laravel-activitylog

Additional context

Exception

Illuminate\ Database \ Eloquent \ JsonEncodingException

Stack Trace


[2022-11-03 17:24:38] local.ERROR: Unable to encode attribute [properties] for model [Spatie\Activitylog\Models\Activity] to JSON: Non-backed enums have no default serialization. {"userId":1,"exception":"[object] (Illuminate\\Database\\Eloquent\\JsonEncodingException(code: 0): Unable to encode attribute [properties] for model [Spatie\\Activitylog\\Models\\Activity] to JSON: Non-backed enums have no default serialization. at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/JsonEncodingException.php:47)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(1232): Illuminate\\Database\\Eloquent\\JsonEncodingException::forAttribute()
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(976): Illuminate\\Database\\Eloquent\\Model->castAttributeAsJson()
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2230): Illuminate\\Database\\Eloquent\\Model->setAttribute()
#3 /var/www/html/vendor/spatie/laravel-activitylog/src/ActivityLogger.php(105): Illuminate\\Database\\Eloquent\\Model->__set()
#4 /var/www/html/vendor/spatie/laravel-activitylog/src/Traits/LogsActivity.php(82): Spatie\\Activitylog\\ActivityLogger->withProperties()
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(421): Domain\\User\\Models\\Membership\\Membership::Spatie\\Activitylog\\Traits\\{closure}()
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(249): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}()
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(189): Illuminate\\Events\\Dispatcher->dispatch()
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1308): Illuminate\\Database\\Eloquent\\Model->fireModelEvent()
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1128): Illuminate\\Database\\Eloquent\\Model->performInsert()
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(978): Illuminate\\Database\\Eloquent\\Model->save()
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/helpers.php(306): Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}()
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(979): tap()
#13 /var/www/html/src/Domain/User/Actions/Membership/CreateMembershipsAction.php(58): Illuminate\\Database\\Eloquent\\Builder->create()
#14 /var/www/html/src/Domain/User/Actions/Membership/CreateMembershipsAction.php(30): Domain\\User\\Actions\\Membership\\CreateMembershipsAction->resolveForBulkCreation()
#15 /var/www/html/src/Domain/User/Actions/Role/CreateRoleAction.php(28): Domain\\User\\Actions\\Membership\\CreateMembershipsAction->execute()
#16 /var/www/html/app/Filament/Resources/RoleResource/Pages/CreateRole.php(24): Domain\\User\\Actions\\Role\\CreateRoleAction->execute()
#17 /var/www/html/vendor/filament/filament/src/Resources/Pages/CreateRecord.php(75): App\\Filament\\Resources\\RoleResource\\Pages\\CreateRole->handleRecordCreation()
#18 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Filament\\Resources\\Pages\\CreateRecord->create()
#19 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#20 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#21 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#22 /var/www/html/vendor/livewire/livewire/src/ComponentConcerns/HandlesActions.php(149): Illuminate\\Container\\BoundMethod::call()
#23 /var/www/html/vendor/livewire/livewire/src/HydrationMiddleware/PerformActionCalls.php(38): Livewire\\Component->callMethod()
#24 /var/www/html/vendor/livewire/livewire/src/LifecycleManager.php(89): Livewire\\HydrationMiddleware\\PerformActionCalls::hydrate()
#25 /var/www/html/vendor/livewire/livewire/src/Connection/ConnectionHandler.php(13): Livewire\\LifecycleManager->hydrate()
#26 /var/www/html/vendor/livewire/livewire/src/Controllers/HttpConnectionHandler.php(20): Livewire\\Connection\\ConnectionHandler->handle()
#27 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(46): Livewire\\Controllers\\HttpConnectionHandler->__invoke()
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#29 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\Route->runController()
#30 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(725): Illuminate\\Routing\\Route->run()
#31 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#32 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#33 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#34 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#35 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#36 /var/www/html/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#37 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#38 /var/www/html/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#39 /var/www/html/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
#40 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Session\\Middleware\\StartSession->handle()
#41 /var/www/html/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#42 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#43 /var/www/html/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#44 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#45 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#46 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\\Pipeline\\Pipeline->then()
#47 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(703): Illuminate\\Routing\\Router->runRouteWithinStack()
#48 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(667): Illuminate\\Routing\\Router->runRoute()
#49 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(656): Illuminate\\Routing\\Router->dispatchToRoute()
#50 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(190): Illuminate\\Routing\\Router->dispatch()
#51 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#52 /var/www/html/vendor/livewire/livewire/src/DisableBrowserCache.php(19): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#53 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Livewire\\DisableBrowserCache->handle()
#54 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#55 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#56 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(36): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#57 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#58 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#59 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#60 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#61 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#62 /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#63 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\HandleCors->handle()
#64 /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#65 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\TrustProxies->handle()
#66 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#67 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(165): Illuminate\\Pipeline\\Pipeline->then()
#68 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(134): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#69 /var/www/html/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle()
#70 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php(16): require_once('...')
#71 {main}
"} 

@Gummibeer
Copy link
Collaborator

Your exception is talking about "Non-Baked enums" which means they don't have a value but only a name.
Can you verify that you use a non baked enum? And if so how Laravel cast stores the non baked enum in database?
Would help to debug, verify and fix the issue.

@pemudakoding
Copy link
Author

Your exception is talking about "Non-Baked enums" which means they don't have a value but only a name. Can you verify that you use a non baked enum? And if so how Laravel cast stores the non baked enum in database? Would help to debug, verify and fix the issue.

Exactly I didn't have value on my enum cause some reason, but I thought,it will be a good idea if we give a fallback if the enum doesn't has value. when we use non baked enum let say I've name with "Monthly" or "Yearly" definitely I just want use it instead of make value so will safe my time, so when i pass to eloquent, eloquent will pass the name, so if the name is "UserRegisteredInYearly" eloquent will save the same also.

@pemudakoding
Copy link
Author

Pached the issue.

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

Successfully merging a pull request may close this issue.

2 participants