Kritim Yantra
Mar 13, 2025
In this beginner-friendly guide, we'll walk through setting up a new Laravel 12 project with Vue 3 and create a simple API call. Perfect for developers starting their full-stack journey!
Create a new Laravel project using Composer:
composer create-project --prefer-dist laravel/laravel laravel-vue-demo
cd laravel-vue-demo
Install Vue 3 and its dependencies via npm:
npm install vue@next @vitejs/plugin-vue
Update vite.config.js
:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
});
Create resources/js/components/PostList.vue
:
<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
const posts = ref([]);
onMounted(async () => {
try {
const response = await axios.get('/api/posts');
posts.value = response.data;
} catch (error) {
console.error('Error fetching posts:', error);
}
});
</script>
<template>
<div>
<h2>Latest Posts</h2>
<ul v-if="posts.length">
<li v-for="post in posts" :key="post.id">
{{ post.title }}
</li>
</ul>
<p v-else>Loading posts...</p>
</div>
</template>
Add a test route in routes/api.php
:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/posts', function () {
return response()->json([
['id' => 1, 'title' => 'Hello Vue!'],
['id' => 2, 'title' => 'Welcome to Laravel!'],
]);
});
1. Update resources/js/app.js
:
import { createApp } from 'vue';
import PostList from './components/PostList.vue';
const app = createApp({});
app.component('PostList', PostList);
app.mount('#app');
2. Modify resources/views/welcome.blade.php
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Laravel + Vue 3 Demo</title>
@vite(['resources/js/app.js'])
</head>
<body>
<div id="app">
<h1>Laravel + Vue 3 Demo</h1>
<post-list></post-list>
</div>
</body>
</html>
Start both servers in separate terminals:
php artisan serve
(defaults to http://localhost:8000
).npm run serve
in the front-end directory (defaults to http://localhost:8080
).
Visit http://localhost:8080
in your browser. You should see the list of posts (initially empty) and your Vue component making an API call!
npm install
if dependencies are missing.php artisan optimize:clear
if changes are not appearing.Congratulations! You've just created your first Laravel + Vue 3 application with API integration. Happy coding! 🚀
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 11, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google