Environment in Laravel for beginners

In the tutorial laravel part 4, we have learned create unit tests and running it. now we will to learnd about Environment variable in laravel, When we create an application, sometimes we need to store configuration values in an environment variable. Laravel has features to make it easier for us to retrieve data from environment variables. why we are store our application configuration in the environment variable ?? because if we write the environment variable in our hardcode, we will difficult to change it later. usually in making an application there are stages, for example running locally first then to development then to QA Environments then to Production Environments, so the configuration of environment variable values will change. Examples of environment variable configurations are database passwords, database names, database user names, database locations and others.

In Laravel we can retrieve the value from the environment variable by using the env(key) or Env::get(key) function.

Table of Contents :

  • Create an Environment variable in unit tests
  • Running Environment variables in unit tests
  • Default Value of Environment

Create an Environment variable in unit tests

For practice we try to create a unit test to check the value of the environment variable in Laravel. open the terminal then make an integration test with the command “php artisan make: test environmenttest”, if it is successful then open the file in the test/Feature/environmenttest.php folder, then we edit the code as below and save.

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class environmenttest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testGetEnv()
    {
        $envvar    =   env('environmentvar');
        $this->assertEquals('this is first evironment', $envvar);
    }
}

If successful, it will look like the image below.

Laravel tutorial - environment
create unit test environment – photo by webbaliseo

Running Environment variables in unit tests

if we run the unit test above to test the environment variable that we just created, it will definitely get an error because the Environment “environmentvar” does NOT exist as shown below.

environment error
environment error – photo by webbaliseo

To create an Environment Variable “environmentvar”, we can use Laravel’s ability to read values from the .env file, that contained in the laravel project that we have created.

open the .env file, if there is no .env file in your project, you can copy the .env.example file into an .env file, then add the code below at the top of the .env file

environmentvar="this is first evironment"

After edit the .env file, The .env file will look like the image below

create environment variable in .env file
create environment variable in .env file – photo by webbaliseo

If we run the unit test that we created now (environmenttest.php) which is located in the test/Feature/environmenttest.php folder, it will look like the image below

run unit test to check the environment variable value
run unit test to check the environment variable value – photo by webbaliseo

From the picture above it appears that it says OK (1 test, 1 assertion), which means that the value of the environment variable “environmentvar” in the .env file is the same as ‘this is the first environment’ in the unit test environmenttest.php file, which is located in the test/Feature/environmenttest.php because we use $this->assertEquals(‘this is first evironment’, $envvar); in our unit tests.

Default Value in Environment

Laravel supports default values for Environment variables, Default values are values that are used when the environment variables that we retrieve are not available. We can use the function env(key, default) or Env::get(key, default)

we will create an example of the default value in the environment in the unit test, type “php artisan make: test defaultvalue” in the terminal to make the integration test, then open the file defaultvalue.php in the folder test/feature/defaultvalue.php then edit and type the code below this

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class defaultvalue extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_default_value()
    {
        $name = env('first', 'Herbert');
        $this->assertEquals('Herbert', $name);

    }
}

Then we run our unit test by pressing the button; ctrl+shift+p then type/select “Better PHPUnit: run” if there is no error it will look like the image below

create environment default value
create environment default value – photo by webbaliseo

the code “$name = env(‘first’, ‘Herbert’);” above is an example of an environment with a default value, which means that if there is no “first” environment value then the “first” environment value will be automatically filled in by default with the “Herbert” value.

Or you can use the env class, like “Env::get(key, default)”, open the defaultvalue.php, that we have created before, the file located in the test/feature/defaultvalue.php folder then edit and type the code below

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\env;
use Tests\TestCase;

class defaultvalue extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_default_value()
    {
        $name = env('first', 'Herbert');
        $this->assertEquals('Herbert', $name);

    }

    // Class env -> Env::get(key, default)
    public function test_default_value2()
    {
        $name2  =   env::get('second', 'terenyi');
        $this->assertEquals('terenyi', $name2);

    }

}

The code “use Illuminate\Support\env;” is the env class that we will import so we can use the code “Env::get(key, default)“, if the unit test above is run it will look like the image below

using env class in environment
using env class in environment – photo by webbaliseo

That’s the laravel tutorial part 5, hope it’s useful, see you in the laravel tutorial part 6 …

You may want to take a look at other laravel tutorials :