How to get Laravel env variable in blade
Hey Folks,
So I have the environment variable, AMP_KEY and I want to fetch that in my blade file so that I can initialize the amplitude library.
AWS_ACCESS_KEY_ID=**
AWS_SECRET_ACCESS_KEY=**
AWS_DEFAULT_REGION=**
AWS_BUCKET=**
AMP_KEY=**
And in my blade file, I have this
<meta name="amp_key" content="{{ env('AMP_KEY') }}">
<script>
var amp = document.getElementsByName('amp_key')[0].getAttribute('content');
</script>
The value of amp is empty. What am I doing wrong?
Best,
Madhu
Don't use env() anywhere except in your con fig files; once config has been cached, there will be nothing available from .env.
If AMP is a third party service, then put the key into config/services.php:
// config/services.php
<?php
return [
// ...
'amp' => [
'key' => env('AMP_KEY'),
],
],
Then in the view:
<meta name="amp_key" content="{{ config('services.amp.key') }}">
Please or to participate in this conversation.