How to Add Custom env Variables in Laravel?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

This article will give you lesson of laravel custom env variables. this lesson will help you laravel environment 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.

Sometime we may require to add new env variable and use it. i will give you two lessons 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 lesson:

Lesson 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);

});

Result:-

xxxxxxxx

Lesson 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);

});

Result:-

xxxxxxxx

now you can check your own.

thank you very much for following up with me.

Tags

Recent Posts

Related Posts