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"

Mike davison
  • 309
  • 4
  • 17
  • 1
    Take a look at some of the files in the `config` folder. Also, you might need to `php artisan config:clear` for new settings to take effect – brombeer Jun 08 '18 at 06:38
  • https://stackoverflow.com/questions/34263107/get-environment-value-in-controller – hungrykoala Jun 08 '18 at 06:38
  • Possible duplicate of [Accessing Laravel .env variables in blade](https://stackoverflow.com/questions/43040967/accessing-laravel-env-variables-in-blade) – Loek Jun 08 '18 at 07:12

3 Answers3

10

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.

Nikhil Joshi
  • 386
  • 2
  • 18
8
<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
Faisal Sarfraz
  • 641
  • 6
  • 18
Kawsar
  • 113
  • 5
3

In Blade you could do something like:

{{ env('APP_URL')

In a controller or PHP Class:

env('APP_URL')
Stan Barrows
  • 873
  • 1
  • 12
  • 27