How to access Laravel env variables in Vue.js | Laravel MIX

By Dillon Smart · · · 1 Comments

Using Laravel and Vue together is a common approach as it allows the MVC framework to handle the logic, and an application-like user interface for the frontend. Learn how to use Laravel environment variables in Vue.js.

MIX Environment Varaibles

Vue js files are compiled by Laravel Mix. To have Mix compile your environment variables from your .env file you must prefix your variable names with mix_.

A good example of this would be setting the API URL for your SPA application, like so:

MIX_API_URL=http://app.test/api

Laravel Environment Variables in Vue JS

Use Laravel environment variables in Vue JS

To use the variables in your Vue JS application, use the following:

process.env.MIX_API_URL

Laravel .env variables are undefined in Vue js? 

If your environment variables are returning undefined, it is likely that you haven’t compiled the assets since you changed the value of the variable. Sometimes it is easy to leave the watcher running. Cancel the current watch command and re-run it.

Laravel Mix Vue JS Watch for changes

Learn more tips for Vue and Laravel!

LaravelVue

1 Comment

Vue 3 Popup Modal Component – Create a Modal - IKnowThatNow

[…] Do you use Vue with your Laravel Application? Learn how to use environment variables in Vue. […]

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Laravel Undefined Constant header_x_forwarded_all after upgrading Laravel version

Updated 1st January 2024

Upgrading to a newer version of Laravel can be exciting, but it can also be nerve racking if you are unfamiliar with the changes in newer version, especially if your upgrading multiple times. Have you received an “Undefined Constant” error in Laravel after upgrading? In this post, I will help you understand the changes under

Laravel 9: How to Add a New Column to an Existing Table in a Migration

Updated 1st October 2023

Laravel has a number of features designed to make developing web applications easier and faster. Migrations in Laravel allows us to manage the structure of our databases. In this post, you will learn how to add a new column to an existing table in a migration. Create a new migration file First, we need to

How to use Laravel Factory in Database Seeders

Updated 16th August 2022

Laravel factories are a great way to add test data to your database in bulk. This saves time, removing the need to write methods to insert data for each column manually. Follow along to learn how to use Laravel Factory in Database Seeders.  To get started, create a new model. The model we will create