I have created a new .env variable:
ALLOW_REGISTER=false
I try to access it in blade like this:
<script>
let ALLOW_REGISTER = "{{ config('app.allow_register') }}";
let ALLOW_REGISTER = "{{ env('ALLOW_REGISTER') }}";
</script>
It is empty. I tried adding the var into config/app.php:
'allow_register' => env('ALLOW_REGISTER'),
After that I did clear all cache. I did run npm run dev and restarted everything. Still empty.
The weird thing is that other .env vars do work, e.g.:
let ALLOW_REGISTER = "{{ env('APP_ENV') }}";
Whats going on here? How can I access the value of ALLOW_REGISTER? Why other .env vars work?
3
Answers
I don't know why, but it only works like this:
Also run
php artisan optimize:clear
if you have laravel 7+Maybe someone can explain why it works only like this and is not accessible directly
Better way to pass these kind of data – is from Controller which returns that view just like this:
And in your view like this:
Note: never use env function in your code – it`s unsafe!
Your issue is that you are not casting to
string
abool
ean, so PHP literally does not append any string.You can check this behaviour by just doing
echo true
andecho false
, you will see nothing is output (try it out in the terminalphp -r 'htmlspecialchars(true);'
andphp -r 'htmlspecialchars(false);'
, you can see no string is returned back.htmlspecialchars
is what it is being used when you do{{ something }}
).So, follow this steps: first you need to add a config, you did end up having
'allow_register' => env('ALLOW_REGISTER'),
insideconfig/app.php
, and that is correct (you can put it in anyconfig/*.php
file that is relevant for you.Then, you need to use
config('xxxx.allow_register')
. Following your example (usingconfig/app.php
) it is going to beconfig('app.allow_register')
.Then, if you are using
php artisan config:cache
in your production server (you should be using it), remember to run it again. If you are using that command locally, please don’t do so, it is going to mess up your tests and more stuff that when developing is just a pain… (in that case runphp artisan config:clear
and never run it again locally).Last step is, you should be using
{{ (string) config('app.allow_register') }}
in your blade view (I would suggest to pass it from the controller, instead of requesting the value on the view, but following your case, your issue is that you literally asked for the value.What do I mean? if
config('app.allow_register')
returnstrue
orfalse
, it will literally do{{ true }}
(or{{ false }}
), and the{{ }}
laravel helper (PHP code) will literally executetrue
(orfalse
), hence doing nothing…What you want to do is escape the value, so it can be inserted as text, so javascript can interpret it. Your solution is:
{{ (string) config('app.allow_register') }}
.You need to cast it to string, and PHP does not automatically casts
true
andfalse
to strings, so they are literally not displayed or writen in HTML.One quick tip: NEVER use
env
outside aconfig
file, because you should be usingconfig:cache
so the config is load faster. When you use that command, it will automatically returnnull
for anyenv(...)
, so it will break your code, more info about this in the docs.