ItSolutionStuff.com

Laravel - How to Get .env Variable in Blade or Controller?

By Hardik Savani • April 16, 2024
Laravel

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 in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

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:

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

echo 'Local Enviroment';

}

You can simple get env file value like this way.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Install Sweetalert2 in Laravel 10 Vite?

Read Now →

Laravel Country List with Flags Example

Read Now →

Laravel 10 Create Custom Helper Functions Example

Read Now →

How to Get All env Variables in Laravel?

Read Now →

How to Add Custom env Variables in Laravel?

Read Now →

How to Get Last Executed Query in Laravel?

Read Now →

How to Get URL Segment in Laravel?

Read Now →

How to Get Current Route Name in Laravel?

Read Now →

How to Add Query String Automatically on Laravel Pagination Links?

Read Now →

How to Get IP Address in Laravel?

Read Now →

How to Create Custom Helper Function in Laravel?

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →

How to Set URL without Http of Other Site in Laravel?

Read Now →