Laravel – How to get .env variable in blade or controller?

Laravel – How to get .env variable in blade or controller?

In this post we will give you information about Laravel – How to get .env variable in blade or controller?. Hear we will give you detail about Laravel – How to get .env variable in blade or controller?And how to use it also give you demo for it if it is necessary.

In this artical, i will give you simple example how to get config variable (env variable) in view file laravel. we can get env variable value in controller and blade file too. we can simple print env file variable by using env() helper.

When i was new to laravel and i was trying to get my google api key from .env file. i created “GOOGLE_MAP_ID” variable in .env file and i need to access on blade file js code. i used that key by using env() function. We can access config variable using env() helper.

Syntax:

env('VARIABLE_NAME');

Example:

env('APP_URL');

You can see bellow simple example how you can get env file variable in blade or controller file.

Blade File:

@if (env('APP_ENV') == 'local')

Local Enviroment

@endif

Controller File:

Also see:How to Get Last Executed Query in Laravel?

if (env('APP_ENV') == 'local'){

echo 'Local Enviroment';

}

You can simple get env file value like this way.

I hope it can help you…

Hope this code and post will helped you for implement Laravel – How to get .env variable in blade or controller?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

17  +    =  21

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US