This is a common issue facing our new Laravel developers mainly because they could not find or did not exist .env files in their laravel setup project.

What is a .env file in laravel.?

.env file is an environment variable file of your laravel framework project. env file contains all configuration variables related to the laravel framework.

For example, you can declare and manage all the environment variables like the below:

APP_NAME = "Laravel CodeZiper Application"

DB_CONNECTION = DATABASE_CONNECTION // mysql
DB_HOST = HOST_NAME
DB_PORT = PORT_NUMBER // 21 or 3306
DB_DATABASE = DATABASE_NAME
DB_USERNAME = USERNAME_NAME
DB_PASSWORD = *******

How to generate a .env file if missing from the laravel project?

First, you need to find .env.example it is in your root directory or you need to download the latest version of laravel. [ Create laravel project with specific version ]

Copy that  .env.example and change the name of that to .env.

After that run the below command to configure your environment key.

Now you can able manage your own environment variables settings.

php artisan key:generate

How to create the env variable in the laravel?

 Below we have added a new env variable.

APP_URL = "https://codeziper.com/" // variable_name = "variable_value"

How to get the env variable in the laravel controller.?

for example, you already have different variables in the .env file. like APP_NAME, 

DB_CONNECTION, DB_DATABASE, etc.

<?php

   $app_url = env("APP_URL", "ENTER_DEFAULT_VALUE_IF_NOT_EXIST_VARIABLE");

?>

How to get the env variable in the laravel blade file.?

when we get a value in the blade file we need to declare with {{ //content }}.

<a href="{{ env('APP_URL') }}" target="_blank">APP_URL</a>

 

Thanks, Regards,

Codeziper and Team.




Leave a reply related to this blog

Tell us about your idea, and we’ll make it happen.

Have a brand problem that needs to be solved? We’d love to hear about it!
Let’s Get Started
up