What Laravel uses APP_KEY for
| Feature | Effect of a new APP_KEY |
|---|---|
| Cookies (EncryptCookies middleware) | all cookies unreadable, users logged out |
| Sessions | existing sessions invalid |
Crypt::encrypt() / encrypt() helper | old values throw DecryptException |
Eloquent encrypted casts | columns can no longer be decrypted |
Signed URLs (URL::signedRoute) | old links fail signature check |
Queued jobs with ShouldBeEncrypted | pending jobs fail |
User passwords (Hash::make) | no effect, hashes do not use APP_KEY |
That table is the reason never to regenerate the key casually: encrypted database columns are lost for good unless you still have the old key.
Rotating APP_KEY in Laravel 11 and later
Since Laravel 11, the encrypter tries the current key first and then each key in APP_PREVIOUS_KEYS. New data is encrypted with the new key; old data stays readable.
APP_KEY=base64:NEW_KEY_FROM_THE_GENERATOR= # comma-separated if there are several APP_PREVIOUS_KEYS=base64:OLD_KEY=
- Deploy with the new
APP_KEYand the old one inAPP_PREVIOUS_KEYS, then runphp artisan config:cache. - Re-encrypt long-lived data (encrypted casts) by loading and saving the models, for example in a one-off command.
- Once nothing depends on the old key, remove it from
APP_PREVIOUS_KEYS.
If the old key leaked, still rotate, but treat the data encrypted with it as exposed. On Laravel 10 and older there is no APP_PREVIOUS_KEYS; you have to decrypt with the old key and re-encrypt in a script.
Generating the key in code or CI
# artisan (writes into .env) php artisan key:generate # print only, do not touch .env php artisan key:generate --show # without PHP/Laravel echo "base64:$(openssl rand -base64 32)"
The OpenSSL line gives exactly the same format as the generator above. For the rest of the Laravel .env (database, Redis, mail), use the Laravel .env generator. For other frameworks see the Django secret key generator and the Rails secret key generator.