Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use values from .env in Laravel 5.6

Tags:

php

laravel-5

Hi ?I am just learning to use Laravel framework and having trouble understanding how to use the values stored in the .env file. I have created a template application using artisan and it works. In the default welcome blade is the code

<div class="title m-b-md">
     Laravel <--/*this text needs to change */
</div>

In the file .env is the lines:

APP_NAME=SomeAppName
APP_ENV=local
APP_KEY=base64:zUm/qFNKTV4gRw6bFcIdOfm5rTzrS8JP1bj2KIz8Rl4=
APP_DEBUG=true
APP_URL=http://localhost

How do i reference the .env file so that the welcome page shows the value of APP_NAME ("SomeAppName") instead of "Laravel"?

p.s have tried {{ getenv('APP_NAME') }} and {{ config('app.name', 'SomeAppName') }} but former gives blank and latter still prints out "Laravel"

like image 412
Mike davison Avatar asked Jun 08 '18 06:06

Mike davison


People also ask

Can I use variables in .env file?

You can set default values for environment variables using a . env file, which Compose automatically looks for in project directory (parent folder of your Compose file). Values set in the shell environment override those set in the . env file.


3 Answers

It seems both of @stan & @yash above answers are partially correct.

Please Follow below links:

  1. https://laracasts.com/discuss/channels/laravel/printing-env-variables-in-view
  2. Accessing Laravel .env variables in blade

According to above what you need to do is as below

<div class="title m-b-md">
      {{ env('APP_NAME') }}
</div>

Then from console you need to clear cache.

php artisan config:clear
php artisan cache:clear
composer dump-autoload

This should work for you.

like image 74
Nikhil Joshi Avatar answered Oct 05 '22 19:10

Nikhil Joshi


<div class="title m-b-md">
     {{ env('APP_NAME') }}
</div>

If your running local environment server. Firstly you restart your local environment server and try this.

And if you enabled config cache than run the commands below

php artisan config:clear

php artisan cache:clear
like image 40
Kawsar Avatar answered Oct 05 '22 18:10

Kawsar


In Blade you could do something like:

{{ env('APP_URL')

In a controller or PHP Class:

env('APP_URL')
like image 31
Stan Barrows Avatar answered Oct 05 '22 17:10

Stan Barrows