SerializableClosure error in Laravel - Your serialized closure might have been modified and it's unsafe to be unserialized
Asked Answered
P

8

38

I am getting this error in my domain where I have used laravel v8 for my site. enter image description here

enter image description here

It shows error in line no 52 in index file: enter image description here

Previously my .env file was not reading. As soon as I fixed that error I got this one now. Please help me solve this error.

Persis answered 30/9, 2020 at 4:10 Comment(5)
how do you resolve this? I'm seeing the same issueTaishataisho
How did you resolve this this issue?Dramaturge
@theodory It was because of App_key in .env file. So, you can generate key using php artisan key:generate. In case it is not working, you can try the following code in cmd: composer run post-root-package-installPersis
Thank you @CherrylRarewings I regenerated the key and everything was fixed.Dramaturge
In my case there was an error in my composer install log. I had an artisan command with a bad classname. Same error however with the serialized closureLionize
K
17

This issue might occur due to change in APP_KEY value in your .env file.

APP_KEY is used for secure data transmission and Laravel recommends to do that by setting it to a random string.

This APP_KEY is used for

  • Encrypting cookies.
  • Creating the signature for signed URLs and queued closures.
  • Encrypting values using the encrypt() and decrypt() helpers.

A encrypted data can be decrypted if you use the same key which was used while encrypting.

So if possible look for a backup .env file and use the same APP_KEY to resolve it.

Kathikathiawar answered 30/9, 2020 at 5:11 Comment(2)
What if I want to change to a new key? Anyway to clear the previous cached content?Hawkeyed
Encrypted data can be stored in cache or databse too. If the concent is such that, you can remove it, you can easily do that but sometimes it can be secrets which are used by your users. In that situation you need a migration from old encryption to the new one.Kathikathiawar
K
88

What solved it for me was running the following in my project folder:

php artisan route:clear

I also ran the below commands before the above command, but the error persisted. It only went away when I did the above. For reference, here are the commands I ran beforehand:

php artisan cache:clear
php artisan config:clear
php artisan view:clear
npm run dev
php artisan key:generate

I also want to note that I am using the following stack:

Laravel v8
Jetstream
Inertia
Vue2
Keating answered 27/2, 2021 at 19:26 Comment(2)
This php artisan route:clear was the solution for meJudicative
I suggest the php artisan optimize command that combines the functionality of both the "cache:clear" and "route:clear" commands and perform other useful actions to improve the app's performance.Lacteal
K
17

This issue might occur due to change in APP_KEY value in your .env file.

APP_KEY is used for secure data transmission and Laravel recommends to do that by setting it to a random string.

This APP_KEY is used for

  • Encrypting cookies.
  • Creating the signature for signed URLs and queued closures.
  • Encrypting values using the encrypt() and decrypt() helpers.

A encrypted data can be decrypted if you use the same key which was used while encrypting.

So if possible look for a backup .env file and use the same APP_KEY to resolve it.

Kathikathiawar answered 30/9, 2020 at 5:11 Comment(2)
What if I want to change to a new key? Anyway to clear the previous cached content?Hawkeyed
Encrypted data can be stored in cache or databse too. If the concent is such that, you can remove it, you can easily do that but sometimes it can be secrets which are used by your users. In that situation you need a migration from old encryption to the new one.Kathikathiawar
E
7

It happened to me when I replaced in production my .env file with the .env from development. It was failing because it had a different APP_KEY, so I had to generate a new APP_KEY on production.

It was solved after run these commands:

php artisan key:generate
php artisan config:cache
php artisan route:cache
php artisan view:cache

Maybe it isn't necessary to clear the views and routes.

Eppes answered 25/2, 2022 at 16:9 Comment(0)
R
5

Sometimes php artisan cache:clear doesn't work..

You should remove manually. Example

rm -rf bootstrap/cache/*.php
Redwine answered 5/10, 2022 at 19:12 Comment(2)
This has helped me in several situations where tests have failed.Nock
This was the solution for me - I tried all others, but this one workedSquirm
T
2

Yes the error often occurs when a new APP_KEY is generated and the changes are not picked up by the server. I'll advise you run this command in the project root directory after the new key generation.

php artisan optimize

It worked well for me!

Terret answered 7/6, 2023 at 15:13 Comment(1)
This ensures all the previous cache is cleared and routes and files reconfigurationTerret
F
0

try this command

 composer update
Furtive answered 7/7, 2022 at 15:36 Comment(1)
Your answer has been flagged as low quality because of its length. It would be best if you explained your solution and perhaps pasted a link to a reference.Sentimental
H
0

If nothing above works then just delete vendor directory and run composer install command.

Horseshit answered 4/9, 2023 at 14:4 Comment(0)
W
-3

In vendor → .env,

change APP_URL to http://localhost.

Waggish answered 1/7, 2021 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.