Laravel Custom ENV Variables Tutorial


Share:
Published By: Admin, Published On: Apr 20, 2021, Category: Laravel 8 Laravel

Hi Dev,

Today,I will learn you how to add custom env variables in laravel. We will example of laravel custom env variables. i explained simply about how to add custom env variables in laravel. i would like to share with you add new custom env variables laravel. Let's get started with laravel create new env variable.

we may require to add new env variable and use it. i will give you two examples of how to adding new env variable and how to use it with laravel application.

Here, I will give you full example for simply custom env variables using laravel as bellow.

Example 1: using env() helper

In this example,first add your new variable on env file as like bellow.

.env
...
  
GOOGLE_API_TOKEN=xxxxxxxx
Use it.
Route::get('helper', function(){
    $googleAPIToken = env('GOOGLE_API_TOKEN');
      
    dd($googleAPIToken);
  
});
Output:
xxxxxxxx
Example 2: using config() helper

In this example,now first add your new variable on env file as like bellow.

.env
...
GOOGLE_API_TOKEN=xxxxxxxx
config/google.php
<?php
  
return [
    'api_token' => env('GOOGLE_API_TOKEN', 'your-some-default-value'),
];
  
Use it:
Route::get('helper', function(){
    $googleAPIToken = config('google.api_token');
      
    dd($googleAPIToken);
  
});
Output:
xxxxxxxx

It will help you....

Share: