skip to Main Content

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


  1. Chosen as BEST ANSWER

    I don't know why, but it only works like this:

        <script>
            @if (env('ALLOW_REGISTER'))
                let ALLOW_REGISTER = true;
            @else
                let ALLOW_REGISTER = false;
            @endif
        </script>
    

    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


  2. Better way to pass these kind of data – is from Controller which returns that view just like this:

    public function index(): View
    {
        return view('some.view', 
            [
               'isRegisterAllowed' => config('app.allow_register')
            ]
        );
    }
    

    And in your view like this:

    <script>
            let ALLOW_REGISTER = !!{{ $isRegisterAllowed }}
    </script>
    

    Note: never use env function in your code – it`s unsafe!

    Login or Signup to reply.
  3. Your issue is that you are not casting to string a boolean, so PHP literally does not append any string.

    You can check this behaviour by just doing echo true and echo false, you will see nothing is output (try it out in the terminal php -r 'htmlspecialchars(true);' and php -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'), inside config/app.php, and that is correct (you can put it in any config/*.php file that is relevant for you.

    Then, you need to use config('xxxx.allow_register'). Following your example (using config/app.php) it is going to be config('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 run php 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') returns true or false, it will literally do {{ true }} (or {{ false }}), and the {{ }} laravel helper (PHP code) will literally execute true (or false), 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 and false to strings, so they are literally not displayed or writen in HTML.


    One quick tip: NEVER use env outside a config file, because you should be using config:cache so the config is load faster. When you use that command, it will automatically return null for any env(...), so it will break your code, more info about this in the docs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search