1

I cannot find how to achieve this. I am looking to access an env value in a page's javascript in Laravel 5.1. Any help is appreciated!

I have tried the following without success:

var AppImagePath = "<?php echo {!! env('APP_IMG_PATH') !!}?>";

var AppImagePath = {!! env('APP_IMG_PATH') !!};

2 Answers 2

4

Try this:

<script>
    var AppImagePath = '{!! env("APP_IMG_PATH") !!}';
    console.log(AppImagePath);
</script>

Using <?php ?> is unnecessary when you are printing values with blade brackets. And in the second one, you forgot the quotes.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi @manix, it does not work. I corrected your syntax as you where missing an apostrophe, but it still does not work. Console.log shows {!! env("APP_IMG_PATH") !!}
Look where I have put the console.log call. If the value is not printed, then you need to check if APP_IMG_PATH is setted at your server.
Also, remeber that the view should ends in .blade.php extension
Got it, brilliant, thanks! I was implementing directly in the js file, but putting it into the template worked perfect. Thanks!
Nice! Glad to help you!
0

Hi this will work perfectly fine
it work for me.

var AppImagePath ='{!!getenv('AppImagePath')!!}';<br><br> 

alert(AppImagePath);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.