Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lozza's avatar
Level 4

Environment variables stopped working in blade templates

In my dev environment on my PC all works great but on my server, for some reason the following code results in an empty string:

{{ env('APP_NAME') }} or {{ env('STRIPE_KEY') }}

This is stopping my stripe payment working :-)

Fairly recently I did a full pull from github and composer/npm installs so possibly I've missed something?

Thanks for help :)

Lozza

0 likes
6 replies
LaryAI's avatar
Level 58

It seems like the environment variables are not being loaded properly on the server. Here are a few steps you can take to troubleshoot and fix the issue:

  1. Check the .env file: Make sure that the .env file exists in the root directory of your Laravel project on the server. Verify that the necessary environment variables (e.g., APP_NAME, STRIPE_KEY) are defined correctly in the .env file.

  2. Clear the configuration cache: Run the following command on the server to clear the configuration cache:

php artisan config:clear

This will ensure that any changes made to the .env file are reflected in the application.

  1. Restart the server: If the above steps don't work, try restarting the server to ensure that the changes take effect.

If the issue persists, it's possible that the server's environment is not properly configured. You may need to consult with your server administrator or hosting provider for further assistance.

Additionally, make sure that you are using the correct syntax to access the environment variables in your blade templates. The syntax you provided ({{ env('APP_NAME') }}) is correct, but double-check that the variable names match exactly with what is defined in the .env file.

Let me know if you have any further questions!

1 like
tykus's avatar

You should not be using environment variables anywhere in your code except in config files, because the .env file is not loaded after config is cached.

https://laravel.com/docs/10.x/configuration#configuration-caching

You can use the config helper instead (referencing the appropriate file and path:

{{ config('app.name') }}
{{ env('services.stripe.key') }}
Lozza's avatar
Level 4

Thanks @tykus I wasn't aware of this as I'm quite new to laravel :)

php config:clear has solved the problem immediately but I would also prefer to move these into config files as you suggest...

So for example, where I need to access an .env constant like STRIPE_KEY from blade templates, should I edit config/view.php and add something like: 'stripe_key' => env('STRIPE_KEY')

Thanks Lozza

tykus's avatar
tykus
Best Answer
Level 104

@Lozza Usually stripe and other services keys are located in the config/services.php config file. The key will be dot-syntax beginning with the filename followed by the path to the value, e.g. services.stripe.key for the following:

// config/services.php
return [
    // ...

    'stripe' => [
        'key' => env('STRIPE_KEY'),
        // other stripe config...
    ],
];

Mark the thread solved if you're all set

Please or to participate in this conversation.