Laravel How to Get .env Variable in Controller and Blade

In this tutorial, I will provide you with insights on how to access .env variables in Laravel within both controllers and Blade templates. Oftentimes, there is a need to retrieve .env variables in these components.

For instance, when working on features like Google Maps integration, you might store API credentials such as app IDs and secret keys in the .env file. Moreover, you can configure various settings like the app URL within the .env file.

Let's explore the methods for retrieving these variables from the .env file in both controllers and Blade templates.

Syntax:

env('VARIABLE_NAME');

Example:

just put the same line in your .env file.

Google_API_Key=XXXXX

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

env('Google_API_Key');

 

Blade File:

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

Controller:

if(env('APP_ENV') == 'local')
{ 
  echo 'Match';
}

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS