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() helperIn this example,first add your new variable on env file as like bellow.
.env... GOOGLE_API_TOKEN=xxxxxxxxUse it.
Route::get('helper', function(){ $googleAPIToken = env('GOOGLE_API_TOKEN'); dd($googleAPIToken); });Output:
xxxxxxxxExample 2: using config() helper
In this example,now first add your new variable on env file as like bellow.
.env... GOOGLE_API_TOKEN=xxxxxxxxconfig/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....