Cannot access env variables in Laravel Cannot access env variables in Laravel laravel laravel

Cannot access env variables in Laravel


A possibility is that config caching is enabled. In that case, only env calls in the config/ dir return a value.

This is cryptically explained in the config docs https://laravel.com/docs/5.5/configuration

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files.


In Laravel 8 there have an issue, you cannot get the env variable direct assign env("URL")

To simplify:

Step 1.) Add your variable to your .env file, for example,

URL="http://google.com"

Step 2.) Create a new file inside of the config folder, with any name, for example,

config/getURL.php

Step 3.) Inside of this new file, I add an array being returned, containing that environment variable.

<?php return ['url' => env('URL') ];

Step 4.) Because I named it "getURL", my configuration 'namespace' is now an example. So now, in my controller I can access this variable with:

$url = \config('getURL.url');

Step 5.) Now you can we env variable in the controller

public function url() {return config('example.url');}

Finally, Clear the cache:

PHP artisan config:cache


try use

php artisan config:clear