Search code examples
laravelphpdotenv

How to Get Environment Value in Config File


Currently, I have an environment variable in my .env file.

MY_NAME_TEST=Testing

How do I pass that variable as part of a path in a config file? I'm trying to get a path similar to below:

base_path('Plugins/Testing/Database/Migrations/')

Notice the "Testing" part of the path in the sample above; I need that name to be different each time.


Solution

  • You're able to retreive the values anywhere in your laravel project by making use of the env function.

    env('MY_NAME_TEST'); // returns "Testing"
    

    In your case it would be used like so:

    base_path('Plugins/' . env('MY_NAME_TEST') . '/Database/Migrations/'
    

    The second parameter the env function takes is a default value if a value is not already set.