DEV Community


Posted on • Originally published at

Laravel How to Get .env Variable in Controller and Blade

In this tutorial I will give you information laravel how to get .env variable in controller and blade. many time we need to required of .env variable in controller and blade file like if you are creating google map integration then you have stored app id and secret key in env file. But if you haven't any idea how to get it in controller or blade file. You can also set app URL and many other things on .env file.

So, let's check how to get those variable data from .env file.

Syntax :

Enter fullscreen mode Exit fullscreen mode

Example :

just put the same line in your .env file.

Enter fullscreen mode Exit fullscreen mode

Access set variable in .env file using env() function.

Enter fullscreen mode Exit fullscreen mode

Blade file

@if(env('APP_ENV') == 'local')
Enter fullscreen mode Exit fullscreen mode


if(env('APP_ENV') == 'local')
  echo 'Match';
Enter fullscreen mode Exit fullscreen mode

So, it is very simple to get variable data from .env file like this.

Discussion (1)

jason2605 profile image

You generally do not ever want to use env() outside of laravel configuration files and should instead strive to use the config() helper instead.

The reason for this is when you go to use laravels config:cache command (which would be used in production) env variables are wiped, so your code above wouldn't work as expected.

See here:

For accessing the current environment you can use the helper facade that laravel provides for you as well: