This article will give you example of add new custom env variables laravel. i would like to share with you laravel create new env variable. you can see laravel custom env variables. We will look at example of laravel environment variables. follow bellow step for how to add custom env variables in laravel.
Sometime 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 6, laravel 7 and laravel 8 application.
So, let's follow both example:
Example 1: using env() helper
now 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
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
now you can check your own.
i hope it can help you...
Do you like below Tutorials ?
- Laravel - chmod(storage/oauth-private.key): Operation failed: Operation not permitted
- Laravel 7.x and 6.x datatables example from scratch
- How to compress PNG image using pngquant in PHP?
- Laravel 7.x and 6.x Chart example using Charts Package
- Guzzle http client GET and POST request example in Laravel 5
- Laravel schema default current timestamp value example
- Laravel 7.x and 6.x Get Site URL Examples
- Convert object to array in laravel 7.x and 6.x example