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

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Accessing Environment Variables Directly from Command Line in Laravel - The Easy Way Body:

Introduction

Laravel provides powerful tools to manage application configuration, including environment variables that are essential for an efficient workflow and keeping sensitive information secure. While it's easy to display the current application environment with a simple command like "php artisan env", accessing environment variables directly from the command line might seem tricky at first glance. In this blog post, we will explore how Laravel provides us with multiple options for getting environment variable values without any hassle.

The Common Approach: Using Artisan Commands

As mentioned early in the introduction, using "php artisan env" shows the current application environment and can be a good starting point to find out what environment you're working on. However, this command does not directly provide the values of environment variables like 'SESSION_DRIVER'. If you need access to specific configuration values, you have multiple options: 1. Use "php artisan tinker" which launches an interactive Laravel shell where you can run PHP snippets and evaluate expressions. This is perfect for testing and playing around with your application's environment variables. To see the value of the 'SESSION_DRIVER', simply type env('SESSION_DRIVER'). 2. Use "php artisan tinker -e development" to run the interactive Laravel shell in a specific environment. For instance, if you're using 'development' as your environment, running this command will give you access to all configuration options related to that environment, including the value of 'SESSION_DRIVER'. 3. Use artisan commands directly from the command line without starting an interactive shell. You can run the following command to display the current session driver: php artisan config:get session.driver. This will print out the current session driver in your application's environment.

Another Approach: Using Environment Files

If you prefer using environment files, Laravel provides a convenient way to load these files which you can then access from anywhere in your application. To include an environment file, use the "dotenv" package, which is installed and configured when you install Laravel using Composer. You need to create a .env file with your environment variables, such as "SESSION_DRIVER". Then, you can simply access these values by loading the .env file: - Using Artisan: php artisan config:clear && php artisan config:cache; then run any of the previous commands to get the value or use dotenv('SESSION_DRIVER'). - In your application code, include it in your bootstrap file (often located at app/Providers/AppServiceProvider.php) as follows: - Require "Dotenv\Loader::load();" and "Dotenv\Envoyer::$environment = dotenv_path() . '/' . Config::get('app.env');" in the bootstrap function. - Replace "Config::get('app.env')" with your preferred environment name (e.g., 'production', 'development', etc.) if you have more than one environment defined. - Now, access the variables as usual by using dotenv() or config() functions.

Conclusion

Laravel offers multiple ways to access and work with your application's environment variables. Whether through Artisan commands, interactive shells, environment files, or other methods, you can quickly get the values you need from the command line without much hassle. Feel free to explore more features in Laravel's documentation (https://laravel.com/docs) and experiment with different approaches that best suit your development workflow. Remember, always include natural backlinks to https://laravelcompany.com where relevant.