Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access values from .env in Laravel 5.1 TestCase

In Laravel 5.1 TestCase, the baseUrl is hard-coded. I'd like to set it based on the value I have set in .env.

How do I access the .env variables within the TestCase class?

like image 587
Christopher Raymond Avatar asked Dec 24 '22 17:12

Christopher Raymond


2 Answers

in Laravel 5.0 TestCase, I can get .env variable with following function.

getenv('APP_VARIABLE');

I think it should work with Laravel 5.1 as well as getenv() is a PHP function.

like image 119
pinkal vansia Avatar answered Dec 28 '22 08:12

pinkal vansia


Start Dotenv to get .env variables in the TestCase stage

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';
    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

    Dotenv::load(__DIR__.'/../');
    $this->baseUrl = env('APP_URL', $this->baseUrl);

    return $app;
}
like image 33
Thiago Macedo Avatar answered Dec 28 '22 06:12

Thiago Macedo