# .env file

A .env file contains environment variables. Ii conatains usually stuff that you wouldn't want the user to see. In addition to the Laravel variables, you can expand this file with your own variables. Think of API keys for external services such as Google Maps, DarkSky, etc.

These syntax rules apply to the .env file:

  • Compose expects each line in an env file to be in VAR=VAL format.
  • Lines beginning with # are processed as comments and ignored.
  • Blank lines are ignored.

# APP_DEBUG

Use APP_DEBUG=true ONLY in a test environment. In a production environment you always use false. See the output if there is an error in the code. On the left with APP_DEBUG=true and on the right with APP_DEBUG=false. APP_DEBUG=true

# APP_URL

Specify the URL for the project. For the test environment APP_URL=http://vinyl_shop.test/ is sufficient.
In production you will of course use your real URL like: APP_URL=https://thomasmore.be.

# DB_...

We use a MySql database for all our projects. Each project uses its own database.
If you have a project myProject, then it is best to use PhpMyAdmin to create a database with the same name myProject.

In the basic configuration you only need to adjust three variables.
Note that the MySql uses port 3306 by default. Because our database is located within the virtual environment of Homestead, we are not allowed to use port 3306, but port 33060..



 
 
 
 
 

# DEFAULT                   # settings for myProject
DB_CONNECTION=mysql
DB_HOST=localhost           # change to localhost, try 127.0.0.1 or 192.168.56.56 if localhost doesn't work
DB_PORT=33060               # add a zero (or change to 2200 if port 33060 is already in use)
DB_DATABASE=myProject       # change to myProject
DB_USERNAME=homestead       # change to homestead
DB_PASSWORD=secret          # change to secret
1
2
3
4
5
6
7

Open the file config/database.php and see how these variables are used within the application.

# MAIL_...


 




 
 

MAIL_MAILER=smtp
MAIL_HOST=localhost                        # replace 'mailhog' with 'localhost'
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="info@vinyl_shop.test"   # replace "hello@example.com" with your mailadres e.g. "info@vinyl_shop.test"# change to your email address
MAIL_FROM_NAME="${APP_NAME}"               # or your name here e.g. "John Doe"
1
2
3
4
5
6
7
8

WARNINGS

  • If your variable contain spaces, surround it with quotes. E.g. MAIL_FROM_NAME="John Doe"
  • If you want a REAL email address to test certain functionalities, use Mailinator (opens new window) e.g. john.doe@mailinator.com.
    Emails sent to Mailinator are public but temporary and will disappear after a few hours.
  • Open the file config/mail.php and see how these variables are used within the application.

# Determine the environment

  • The current application environment (local, staging or production) is determined via the APP_ENV variable from your .env file
  • You may access this value via the App::environment() facade:
if (App::environment('local')) {
    // The environment is local
}

if (App::environment(['local', 'staging'])) {
    // The environment is either local OR staging...
}

if (App::environment('production')) {
    // The environment is production...
}
1
2
3
4
5
6
7
8
9
10
11
  • You may also use:
    • The @env() Blade directive to conditionally display content based on the current environment
    • The @production() directive to display content only when the application is in production
@env('local')
    // The environment is local
@endenv

@env(['local', 'staging'])
    // The environment is either local OR staging...
@endenv

@production
    // The environment is production...
@endproduction
1
2
3
4
5
6
7
8
9
10
11

# Access variable with config('variable')

REMARKS

  • There are different ways to access the variables in the application but Laravel recommends (opens new window):
    • Use the env() helper function ONLY if you need to access the variable in a config file (e.g. config/database.php)
    • Use the App::environment() method to check the current environment (e.g. in a controller or in a view) e.g.
      • if (App::environment('local')) { ... }
      • if (App::environment(['local', 'staging'])) { ... }
    • Use the config() helper function to access the variable in the application (e.g. in a controller or in a view) e.g.
      • config('app.name'): returns the value of the name variable in the config/app.php file
      • config('app.timezone') returns the value of the timezone variable in the config/app.php file
      • config('mail.from.address'): returns the value of the from -> address variable in the config/mail.php file

You may add additional variables to .env. Think of API keys from external services such as Google maps (opens new window) or OpenWeatherMap (opens new window).
All these variables can now be used in the application at any time. For example in the router, in a controller and even directly in Blade. Here is an example:



 



// web.php
Route::get('mail', function () {
    $me = ['name' => config('mail.from.name')];
    return view('mail', $me);
})->name('mail');
1
2
3
4
5


 

 
 




<!-- resources/views/mail.blade.php -->
...
<h2>I'm  {{ $name }}</h2>
<p>You can contact me at
  <a class="text-sky-600 underline" href="mailto:{{ config('mail.from.address') }}">
    {{ config('mail.from.address') }}
  </a>
</p>
...
1
2
3
4
5
6
7
8
9

Contact John Doe

# Access variable with config('key')

Last Updated: 10/28/2022, 8:43:10 AM