Table of Contents
Introduction
Laravel and Vue.js are a very robust & powerful combination for building modern web applications. Laravel, a PHP-based framework, is known for its simplicity, flexibility, and strong backend capabilities, while Vue.js is a progressive JavaScript framework used for building rich user interfaces. Laravel and Vue JS integration allows developers to harness the strengths of both frameworks, creating full-stack applications that are both robust and user-friendly.
In this article, we will explore how to integrate Laravel with Vue.js, step-by-step, to build dynamic web applications. Whether you are new to Vue.js or already familiar with Laravel, this guide will provide a clear understanding of how to make the two work together seamlessly.
Why Laravel with Vue.js Integrate is required?
Before diving into the technical aspects, it’s essential to understand why integrating Laravel with Vue.js is a good choice. Here are some benefits:
- Full-stack development: Laravel handles the backend logic, data management, and API creation, while Vue.js manages the frontend interactions and user experience.
- Reactive UI: Vue.js provides a reactive, component-based structure, allowing developers to create interactive user interfaces.
- Single-page applications (SPAs): Vue.js can handle client-side rendering, making it ideal for building SPAs where Laravel serves as an API.
- Simplified workflow: Vue.js components can be seamlessly integrated into Laravel Blade templates, reducing the need for separate frontend frameworks or tools.
- Easy scaling: This combination makes it easy to scale an application over time by managing the frontend and backend separately.

Step 1: Setting Up Laravel with Vue.js
To integrate Laravel with Vue.js, the first step is to set up a new Laravel project. If you haven’t installed Laravel, you can install via Composer:
If you don’t know how to install Composer click here.
composer create-project --prefer-dist laravel/laravel laravel-vue-integration
Navigate into your project directory:
cd laravel-vue-integration
Laravel comes pre-configured with Vue.js in its package.json
file. You can check this by opening the package.json
file, where you’ll see Vue.js as a dependency.
Run the following command to install all the frontend dependencies, including Vue.js:
npm install
This installs Vue.js and other JavaScript dependencies required for your project. Now, to ensure everything is working correctly, compile the frontend assets using:
npm run dev
This command compiles your JavaScript and Vue components, and you can now start using Vue.js in your Laravel project.
Step 2: Configuring Webpack and Vue Components
Laravel uses Webpack through Laravel Mix to handle asset compilation. By default, Vue.js is already integrated into Laravel, but you can customize it according to your needs.
In your resources/js/app.js
file, you’ll notice Vue is imported at the top:
require('./bootstrap');
import { createApp } from 'vue';
const app = createApp({});
app.mount('#app');
This is where Vue components are managed. Now, create a Vue component within the resources/js/components
directory. Laravel includes an example ExampleComponent.vue
file, but you can create a custom one.
Create a new Vue component named AppComponent.vue
:
<template>
<div>
<h1>Laravel and Vue.js Integration</h1>
<p>This is a simple Vue component integrated into Laravel.</p>
</div>
</template>
<script>
export default {
name: 'AppComponent'
}
</script>
<style scoped>
h1 {
color: #3490dc;
}
</style>
Now, you can import and register this component in your app.js
file:
import { createApp } from 'vue';
import AppComponent from './components/AppComponent.vue';
const app = createApp({
components: {
AppComponent
}
});
app.mount('#app');
Step 3: Using Vue Components in Laravel Blade Templates
Laravel uses Blade as its default templating engine. Vue components can be integrated directly into Blade views.
To render a Vue component within a Blade file, you need to specify a DOM element that Vue.js will mount. For example, in the resources/views/welcome.blade.php
file, add the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laravel Vue Integration</title>
</head>
<body>
<div id="app">
<app-component></app-component>
</div>
<script src="{{ mix('js/app.js') }}"></script>
</body>
</html>
The id="app"
div is where Vue will render the components. You’ll notice that we’ve referenced app-component
, which is the Vue component we created earlier.
Run the development server using:
php artisan serve
Visit the application in the browser (http://127.0.0.1:8000
), and you should see the Vue component rendered on the page. The result will display a heading and a paragraph from the AppComponent.vue
file.
Step 4: Passing Data from Laravel to Vue Components
One of the significant advantages of using Vue.js with Laravel is the ability to pass dynamic data from the server to the client. This can be done by passing data from Blade templates into Vue components.
To demonstrate this, modify your Blade file to pass data as a prop to the Vue component. In the welcome.blade.php
file, pass data like this:
<div id="app">
<app-component :message="'Hello from Laravel!'"></app-component>
</div>
Now, modify the AppComponent.vue
file to receive this prop:
<template>
<div>
<h1>{{ message }}</h1>
</div>
</template>
<script>
export default {
props: {
message: {
type: String,
required: true
}
}
}
</script>
When you reload the page, the message “Hello from Laravel!” will be displayed through the Vue component. This shows how easy it is to pass data between Laravel and Vue.js.
Step 5: Handling HTTP Requests with Axios
In many applications, you’ll need to make HTTP requests to fetch or update data from the server. Laravel and Vue.js work well together for this purpose, especially when using the Axios library for making AJAX requests.
First, ensure Axios is installed and available in your project. Axios is included by default in a Laravel project, but you can check the resources/js/bootstrap.js
file to confirm:
window.axios = require('axios');
Let’s assume you have a route in your Laravel backend that returns a JSON response:
Route::get('/api/message', function () {
return response()->json(['message' => 'Hello from Laravel API!']);
});
In your AppComponent.vue
file, you can make an Axios request to this route:
<template>
<div>
<h1>{{ apiMessage }}</h1>
</div>
</template>
<script>
export default {
data() {
return {
apiMessage: ''
};
},
mounted() {
axios.get('/api/message')
.then(response => {
this.apiMessage = response.data.message;
})
.catch(error => {
console.log(error);
});
}
}
</script>
When the component is mounted, it sends a GET request to the /api/message
endpoint and updates the apiMessage
data property with the response.
Step 6: Using Vue Router for SPA Development
Vue Router allows you to create Single Page Applications (SPAs) where content is dynamically loaded without refreshing the page. To enable routing, install the Vue Router package:
npm install vue-router
In your app.js
file, import and configure Vue Router:
import { createApp } from 'vue';
import { createRouter, createWebHistory } from 'vue-router';
import AppComponent from './components/AppComponent.vue';
const routes = [
{ path: '/', component: AppComponent }
];
const router = createRouter({
history: createWebHistory(),
routes
});
const app = createApp({});
app.use(router);
app.mount('#app');
Now, define multiple components and routes as needed, and Vue Router will handle the navigation between them without reloading the page.
Conclusion
Integrating Laravel and Vue.js provides a flexible, scalable approach to full-stack development. Laravel’s robust backend, paired with Vue.js’s reactive and dynamic frontend capabilities, makes building modern web applications straightforward and enjoyable. Whether you’re creating a single-page application or simply adding dynamic elements to your Laravel project, Vue.js is a perfect companion.
By following this guide, you should now have a clear understanding of how to set up and integrate Vue.js with Laravel, pass data between them, and make HTTP requests to build interactive and data-driven web applications.