How to Get env Value in Laravel Project

Sovary November 3, 2022 311
1 minute read

This short article will help you to get all variables which contain in .env file. We are going to give you an example of how to list all environment variables in Laravel project. The article will guide you example of Laravel how to get all environment  variables which exist in .env file. You can add new key or existing key to the .env file, and those values will display or we can get the value throw the code below. You can apply this code below with Laravel 6, Laravel 7, Laravel 8, Laravel 9 as well.

We can access all variable values in Laravel with $_ENV will return all variables. So let's check below the code with example.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class TestController extends Controller
{

    public function index(Request $request)
    {
        // Get all variables in .env file
        $env = $_ENV;
 
        // Get value by specific key
        $db = $_ENV['DB_DATABASE'];
  
        dd($env , $db);
    }
}

This is end article, hope this tutorial help you and hope you can learn something with this page. Have a nice by.
You might also like...

 

Laravel  PHP  Laravel 9 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him