Laravel Artisan Key Command: The key:generate Command

December 7, 2016 —John Koster

The key:generate command is used to generate a random key. This command will update the key stored in the application's environment file. The command also supports an optional --show flag. Specifying this flag will simply show the generated key instead of updating any configuration or environment files.

The following example demonstrates how to update/change the application's key:

1php artisan key:generate
Regenerating the Application Key

It is important to note that if the encryption key needs to be regenerated after an application has been in use, any data that was previously encrypted using the old key cannot be decrypted using the new encryption key. While regenerating the encryption key is not recommended, there are some scenarios where it is beneficial, if not absolutely critical to do so.

When these scenarios arise, the problems can be overcome by decrypting the old data and reapplying the encryption using the new encryption key.

The following example will simply display the generated key instead of generating it:

1php artisan key:generate --show

The output will be similar to the following, but the key will most likely be different:

1base64:lg1m/12MHBbBpiWTXjot98Q9MP/nSzPrvLEU2beD+2Y=

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.