如何在javascript中访问laravel

如何在javascript中访问laravel

本文介绍了如何在javascript中访问laravel .env变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Laravel应用程序中将Algolia用于我的搜索引擎,因此无论何时创建记录,都会将其发送到Agolia数据库.当然,我想将Algolia数据库中的数据与用于本地开发的测试集和用于实时网站的生产集分开.我已经定义了JavaScript文件中正在使用的Algolia数据库索引.

I am currently using Algolia for my search engine inside a Laravel application, so whenever a record is created it is also sent to the Agolia database. Of course I want to separate my data in the Algolia database with a testset for local development and a production set for the live website. I have defined which indices of my Algolia database are being used in a JavaScript file.

现在,我想知道如何对APP_ENV变量做出相应的反应以根据当前环境进行更改?显然,我需要在JavaScript中的 if 语句中放入内容,但是如何使javascript正确访问.env变量?

Now I am wondering how do I react accordingly to my APP_ENV variable to change it depending on my current environment? Obviously I need to put stuff into an if statement in my JavaScript but how do I make my javascript access the .env variables properly?

希望有人能帮助我.

干杯.

推荐答案

您可以在视图中将env变量作为javascript字符串回显:

You can just echo out the env variable as a javascript string in your view:

<script>

var name = '{{ env('NAME') }}';

alert(name);

</script>

您的.env文件如下所示:

And your .env file would look like this:

NAME =亚历克斯

NAME=Alex

这篇关于如何在javascript中访问laravel .env变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:13