Laravel env() or config() to get environment variable on command line

Laravel env() or config() to get environment variable on command line

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Runtime Configuration from the Command Line in Laravel

While php artisan env is a convenient utility for inspecting the current application environment, it falls short when you need to inspect specific configuration values like SESSION_DRIVER. Developers often find themselves in scenarios where they suspect a misconfiguration in an environment variable or a config file, but the standard debugging tools only show the broad environment tag.

To retrieve the value of env('KEY') or config('package.key') directly from the terminal, you must bypass the artisan commands and interact with Laravel's internal service containers and cache mechanisms using CLI arguments. This approach is necessary because the configuration system in Laravel is designed to be resolved at runtime, not statically loaded by default shell scripts.

Inspecting Raw Environment Variables

The most direct way to check what value env('SESSION_DRIVER') returns is to access the underlying environment variable directly from the shell. Since Laravel's env() helper simply proxies to PHP's getenv() function (or reads the .env file via the DotEnv loader), you can replicate this behavior using standard shell commands.

If you are running on a Unix-based system, the command echo $SESSION_DRIVER will display the value if it is set in your session or environment. However, Laravel loads these variables from a .env file before booting the application. If you are not currently running the PHP process within that specific shell session, simply echoing the variable might return empty.

To force the inspection of the values loaded by Laravel's DotEnv loader without starting the full application, you can use the php -v flag combined with environment dumping, but a more reliable method involves leveraging the php artisan tinker REPL or creating a temporary script that resolves the configuration array. A common pattern developers use is to create a small PHP file in the root directory:

// dump-env.php
<?php

require __DIR__.'/vendor/autoload.php';

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$app->bootstrapWith([
    \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
]);

$env = $app['config'];
echo $env['SESSION_DRIVER'] ?? 'Not set';

Executing php dump-env.php will output the value exactly as Laravel sees it after parsing the .env file. This is crucial because shell variables might not be exported to the child process, but Laravel's loader ensures they are read correctly.

Using Tinker for Configuration Inspection

For a more interactive experience without writing temporary files, you can use Laravel Tinker. The command php artisan tinker drops you into an interactive PHP session where you have access to the application instance.

Once inside the Tinker prompt:

php artisan tinker
>>> $app = new Illuminate\Foundation\Application(getcwd());
>>> $app->bootstrapWith([ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class ]);
>>> echo config('session.driver');

This sequence initializes the application context and loads the environment variables before resolving the configuration. This method is particularly useful when you need to test multiple keys quickly. You can chain commands or exit Tinker with exit once the value is confirmed. This approach avoids the overhead of starting the web server or running unit tests, making it an efficient troubleshooting step for deployment issues.

Understanding Configuration Resolution Pitfalls

When attempting to debug configuration values via the CLI, a common pitfall involves understanding the difference between environment variables and configuration arrays. The config('session.driver') helper resolves the value from the config/session.php file, which may contain fallbacks or default values if the environment variable is missing. Conversely, env('SESSION_DRIVER') returns null if the variable is not defined in the .env file, regardless of what the config file says.

Developers sometimes assume that changing a value in the .env file immediately affects the running application without restarting it. However, configuration arrays are cached for performance. If you modify SESSION_DRIVER in the .env file and then run your CLI commands, you might still see the old value if the config cache is active.

To ensure you are seeing the live changes, you must clear the configuration cache before inspecting:

php artisan config:clear

This command ensures that the next time config() is called, Laravel re-reads the files and environment variables from scratch. Failing to clear this cache when debugging environment-specific issues is a frequent source of confusion during local development or staging deployments.

When to Use Official Resources

If you require deeper insights into how configuration caching interacts with your specific deployment strategy, or if you need assistance integrating these debugging workflows into your CI/CD pipeline, resources like https://laravelcompany.com can provide guidance on advanced Laravel maintenance strategies. While the core logic for inspecting environment variables remains consistent across versions, understanding the nuances of cache invalidation and application bootstrapping is essential for maintaining a stable deployment environment.

Our network