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

Brainmaniac's avatar

Can't use env('MY_VARIABLE') in javascript

Hello!

I recently updated Laravel from 5... to 6...

Now when I click my buy-buttons I do not get redirected to the stripe purchase page.. On the server. Locally to the test links in stripe it works just fine!

I have stripe integration like this on a blade, the script includes:

var stripe = Stripe('{{env('STRIPE_PUB_KEY')}}');

This works fine locally rendering my stripe public key that I have in my .env but on my server it does not work.. On both server and locally I have Laravel v. 5.7.28 php version server 7.3.12 php version locally 7.4.4

Yes, I have a variable in both my .env on server and locally called STRIPE_PUB_KEY...

This drives me nuts.. What can be the difference. Can it really be the php version?

0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

You should not pull env variables directly.

create a config file that pulls in .env variables and then use that in your code. This way you can use the cached config.

1 like
Brainmaniac's avatar

Thank you Snapey! I will look in to this. Since my upgrade to 5.7 I can no longer access a lot of stuff from my .env on the server.... am using forge. But locally it's fine. 😰

Please or to participate in this conversation.