Dev Duniya
Mar 19, 2025
In this post, we will learn how to install Vue js 3 in laravel 9?. This post shows you how to install vue 3 in laravel 9. If you want to see an example of installing vue 3 in laravel 9 then you are in the right place. Laravel 9 is the latest version of the laravel framework at the writing of this article. As you know Laravel is the most popular PHP framework and it's easy to use scale, and flexible. Vue js is a progressive framework for building user interfaces and it is lightweight and easy to use and learn. Vue 3 is the latest version of the Vuejs Framework and growing rapidly.
By the end of this post, you’ll have created a Vue 3 an Laravel 9 project, built one component and connect it with laravel blade file.
Use the following steps to install vue 3 in the laravel 9 application.
Open your terminal and navigate where you want to install the laravel application. To install the laravel 9 framework you need to execute the following command.
composer create-project --prefer-dist laravel/laravel Laravel9Vue3Project
After installing the project, go to the project folder, and in the terminal execute the following command to install the front-end dependencies. you need node.js to be installed in your system to check the node.js version just write node -v
and npm -v
. if the node is not installed on your machine please visit this link
npm install
Now after installing npm we need to install vue 3, for that execute the following command in the terminal. vue-loader is a loader for webpack that allows you to author Vue components in a format called Single-File Components . vue-loader@next is a loader that is for webpack to author vue components in single-file components called SFCs.
npm install vue@next vue-loader@next
Webpack is a module bundler for modern JavaScript applications. Open webpack.mix.js and copy-paste the following code. Here mix() helper method helps to generate the public/css/app.css and public/js/app.js files and correct path for us. mx() helper method also recommends proper file name according to versioning.
// webpack.mix.js
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.vue()
.postCss('resources/css/app.css', 'public/css', [
//
]);
Now after installing the vue 3 we need to compile the assets for that run the following command and it will compile your resources/js/app.js file and saved it to public/js/app.js.
npm run dev && npm run watch
open resources/js/app.js here you create an instance of the vue 3 projects firstly you import { createApp } from 'vue' and createApp takes a parameter here we have passed App. vue file which is the main file responsible for the vuejs content.
// app.js
require('./bootstrap');
import {createApp} from 'vue'
import App from './App.vue'
createApp(App).mount("#app")
Under the js folder create a file name 'App. vue' and write the following content for this example let's write simple "hello vue 3" you can change it according to your requirements. Vue allows you to declaratively bind the rendered DOM to the underlying Vue instance's data Under the hood, Vue compiles the templates into Virtual DOM render functions.
<template>
Hello vue 3
</template>
Now we need to create a blade file where we'll load vue3 code for that create a new file in the resources/views folder name vue.blade.php. Link app.css file in header section with the help of asset() helper which points to the public folder. Add compiled js file from public/js/app.js in the vue.blade.php file It will load all the js you need to run the vue js code. you can add code to the app.js in the resources folder and compile it, it creates an app.js file in the public folder.
{{-- resources/views/vue.blade.php --}}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 9 Vue 3 - DevDuniya.com</title>
<link rel="stylesheet" type="text/css" href="{{ asset('css/app.css') }}">
</head>
<body>
<div id="app"></div>
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
Laravel routes folder is responsible for the routing in laravel it navigates to the correct view or controller path from here. Inside the routes folder, api.php is responsible for all APIs in laravel. If you wanted to create a crud operation in vuejs then you need to create some APIs to connect your vuejs project with laravel, APIs help to connect the front-end with the back-end, for this example, we don't need APIs we just need to update web.php file Open routes/web.php and load the vue.blade.php file on the home page.
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('vue');
});
Now execute the following command from your terminal. It runs the project on localhost 8000 port by default but you can change it also if you are running another project on the same port number.
php artisan serve
and navigate to the following link http://localhost:8000/
How To Install Vue 3 In Laravel With Vite
That’s it, I hope this article provides you with the necessary information about laravel 9 and vue 3 to build your own app that uses Laravel 9 and Vue 3.
If I forgot or missed something or any ideas for future posts on this topic, let me know in the comments.
I'd love to know!