yyii
yyii

Reputation: 143

Undefined variable: _ENV in Laravel 5.3

I was testing my system with Laravel 5.3:

For past few weeks my system was working fine. Since last weekend, I encountered the same error TWICE as below:-

Notice: Undefined variable: _ENV in C:\NewFolder\htdocs\project\vendor\vlucas\phpdotenv\src\Loader.php on line 303

Warning: array_key_exists() expects parameter 2 to be array, null given in C:\NewFolder\htdocs\project\vendor\vlucas\phpdotenv\src\Loader.php on line 303

Loader.php:

public function getEnvironmentVariable($name)
{
    switch (true) {
        case array_key_exists($name, $_ENV): // line 303 here
            return $_ENV[$name];
        case array_key_exists($name, $_SERVER):
            return $_SERVER[$name];
        default:
            $value = getenv($name);
            return $value === false ? null : $value;
    }
}

.env

APP_ENV=local
APP_KEY=base64:oTU0Ok1mmE6x0qEosGKhCSxpQLAlBAnNreH7sFAKkdM=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=new_db
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_KEY=
PUSHER_SECRET=
PUSHER_APP_ID=

Anyone have any idea on why is this happening? Thanks

The version I used:

Upvotes: 10

Views: 5747

Answers (4)

Alex Garcia
Alex Garcia

Reputation: 11

Old question but at 2021 found same error and solved:

Use https://github.com/lazychaser/laravel-nestedset in Laravel 7

Solution:

Instead of @foreach in your blade templates

use:

@php
foreach(){}
@endphp

Upvotes: 1

New Alexandria
New Alexandria

Reputation: 7324

This is clearly an error outside of the scope of your code; it is a change with your dependencies or your system configuration. You should consider rebuilding your dependency cache, and rolling back to old version of dependencies prior to the date when the error started.

As @MahdiYounesi said, start with the phpdotenv dependency.

Upvotes: 0

yyii
yyii

Reputation: 143

After some finding and research, I found there are some similar issues from Github #8191.

I able to get rid of the error by running php artisan config:cache. Means that Laravel will read the environment variable from the config file rather than reading the environment variable directly from .env

Upvotes: 1

Phil
Phil

Reputation: 165059

The only reason the $_ENV super-global should not be defined is if it has been removed from the variables_order configuration.

Check your php.ini (or any other relevant config files) for the variables_order property. For $_ENV to be set, it must include an "E". The default setting is

variables_order = "EGPCS"

If you're having trouble locating the configuration, a <?php phpinfo() ?> dump should also show the value and config file locations.

variables_order is a PHP_INI_PERDIR mode setting, meaning...

Entry can be set in php.ini, .htaccess, httpd.conf or .user.ini (since PHP 5.3)

The characters stand for

  • E - $_ENV
  • G - $_GET
  • P - $_POST
  • C - $_COOKIE
  • S - $_SERVER

Upvotes: 5

Related Questions