How to Get env Value in Laravel Project

Sovary November 3, 2022 480
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him