How to call env variable in Laravel controller

Updated at 06-Jul-2022, By samar

How to call env variable in Laravel controller

In this session, we are going to try to solve the "How to call env variable in Laravel controller" puzzle by using the computer language.

I have some env variable in my .env file. I want to get the value of these env variable in my Laravel controller. Is there any way to call the env variable to controller.
  • Get the env variable value in controller and view blade file

    env('APP_URL');
    
    //Output
    http://localhost:8000

    Demo code:

    Route::get('/get-env', function(){
        echo env('APP_URL');
    });

    Output may be different in your case.

    You can use above code snippet to get the value of env variable and you can call it in your controller and blade (view) file.

  • config('app.url');
    
    http://localhost:8000

    This code snippet will return the app URL. Which exists in config/app.php file.

Back to code snippet queries related laravel