I can access the config data in .blade
using
{{ config('config.variable') }}
However, I have no idea how to access the config data in .js
file. Can anyone give me some suggestion?
I can access the config data in .blade
using
{{ config('config.variable') }}
However, I have no idea how to access the config data in .js
file. Can anyone give me some suggestion?
2 Answers
Reset to default 3Type 1
You can use the below code to access environment variables in your
.env
injs
.var name = '{{ env('CONFIG_FILE_NAME')}}'; console.log(name);
Type 2
Or else You may inject environment variables into your
webpack.mix.js
script by prefixing the environment variables in your.env
file withMIX_
.process.env.MIX_CONFIG_FILE_NAME
Please Refer Documentation for further reference.
Javascript performed on client-side, and .blade.php on server-side. So, you can only write something like
<script>
var cfg = {{ config('config.variable') }};
</script>
in your .blade.php file to make it accessable from .js files. Or using method that is mentioned in the documentation by providing MIX_ prefix to variables in the .env file.