Avishake
Avishake

Reputation: 470

Have Undefined index while run the cron job in Laravel 5.X with Liebig

I have a strange error in my cron job. At the beginning while I am running the cron job its working fine. At that time I am define the variables normally. Like, $ck_host='abc';. But now I change the code and access the same variable from the .env file of laravel. Like, $ck_host=$_ENV['CK_HOST'];. When I am running in my browser it works fine. But in the cron job, it says undefined index: CK_HOST. I have attach the image of the log.

enter image description here

Note:

I Think:

The problem is where I call the .env file in the code. This corn vendor is not able to use the function. $dotenv = new Dotenv\Dotenv($doc_root); $dotenv->load(); $doc_root contain the path of the env file. Please note once again, all the files are working in the browser but when I run the cron I am writing a log file also. The error come there.

Please help me. Or tell me how will I use .env file without any vendor of laravel.

Thanks in advance for help.

Upvotes: 0

Views: 411

Answers (1)

Avishake
Avishake

Reputation: 470

At last I found the answer of my question.

$_ENV is an associative array of variables passed to the current script via the environment method. See.

But Laravel overwrite the variable and use its own method. So, when this file is running individually then there is no error. But when its run via Laravel, in this case via Laravel Cron method, its overwrite and didn't find those indexes.

So as a solution use getenv function. Like, $ck_host=getenv('CK_HOST');. It will gets the value of an environment variable. And if you run individually or via Laravel in both the case there is no error.

Upvotes: 0

Related Questions