Crypcode
Crypcode

Reputation: 188

Laravel env() value null

I'am trying to retrieve EXAMPLE_URL=www.google.com from .env in controller and everytime get null. Where is the problem because the same code works on another application. This function doesn't work after php artisan cache:clear.

Controller code

    $hostname = env("EXAMPLE_URL");

    dump($hostname);

Upvotes: 1

Views: 2117

Answers (2)

Ayoub Bousetta
Ayoub Bousetta

Reputation: 316

Always use the app file as the middleman

see : https://laravel.com/docs/9.x/configuration

Upvotes: 1

Ron van der Heijden
Ron van der Heijden

Reputation: 15080

You should not use env() outside of the config files.

Read: https://laravel.com/docs/8.x/configuration

You should add the env variable to a config file and use config('example.url');.

The example.php would look like:

return [
    'url' => env('EXAMPLE_URL', 'https://example.com'),
];

Upvotes: 8

Related Questions