Reloading .env variables without restarting server (Laravel 5, shared hosting)

In Laravel 5, you can use the php artisan config:cache command to compile all of your configuration into a single file, which will be loaded quickly by the framework. This will allow you to make changes to your environment variables without having to restart the server.

To clear the configuration cache, use the php artisan config:clear command.

Note that this will only work if you are using the file driver for your configuration cache. If you are using a different cache driver, you will need to clear the cache using the appropriate method for that driver.

You can also use the php artisan tinker command to update the environment variables in your .env file and have them take effect immediately. Just use the env() function to retrieve and update the values.

Watch a course Learn object oriented PHP

For example:

$env = env();
$env['APP_DEBUG'] = true;
$env['APP_URL'] = 'http://example.com';

Then, you can use the putenv() function to update the environment variables in the current process:

foreach ($env as $key => $value) {
    putenv("$key=$value");
}

Keep in mind that these changes will only affect the current request, and will not be persisted across requests. If you want to make these changes permanent, you will need to update the values in your .env file and then run the php artisan config:cache command to compile the updated configuration.