Kritim Yantra
May 03, 2025
In this blog post, we’ll walk through building a simple full-stack app by creating an API with Laravel 12 and consuming it using Nuxt 3—the latest version of the powerful Vue.js framework. Whether you're a Laravel backend developer looking to expand into frontend or a Nuxt user wanting to connect with a robust backend, this guide is for you.
We'll build a simple Post
API using Laravel and display the posts in a Nuxt 3 frontend.
Make sure the following are installed:
composer create-project --prefer-dist laravel/laravel:^12 laravel-api
cd laravel-api
php artisan install:api
This installs Laravel with API scaffolding, including api.php
routes and Laravel Sanctum.
.env
Set up your database in .env
:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_api
DB_USERNAME=root
DB_PASSWORD=
Then run the migrations:
php artisan migrate
php artisan make:model Post -mf
Update the migration file:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
Run the migration:
php artisan migrate
php artisan make:controller PostController --api
Update the controller:
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
return Post::all();
}
}
In routes/api.php
:
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
You can also use:
Route::apiResource('posts', PostController::class);
Run the Laravel dev server:
php artisan serve
Open your browser or Postman and visit:
http://127.0.0.1:8000/api/posts
You should see an empty array or your list of posts.
npx nuxi init nuxt-app
cd nuxt-app
npm install
This scaffolds a Nuxt 3 project using Vue 3.
Create a .env
file:
NUXT_PUBLIC_API_BASE_URL=http://127.0.0.1:8000/api
Then update your nuxt.config.ts
:
export default defineNuxtConfig({
runtimeConfig: {
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE_URL
}
}
})
Create or edit pages/index.vue
:
<script setup lang="ts">
const config = useRuntimeConfig()
const posts = await $fetch('/posts', {
baseURL: config.public.apiBase
})
</script>
<template>
<div class="p-6">
<h1 class="text-2xl font-bold mb-4">Posts</h1>
<ul class="space-y-4">
<li v-for="post in posts" :key="post.id" class="p-4 border rounded-lg">
<h2 class="text-xl font-semibold">{{ post.title }}</h2>
<p>{{ post.content }}</p>
</li>
</ul>
</div>
</template>
You now have:
http://127.0.0.1:8000/api/posts
Install Axios:
npm install @nuxtjs/axios
Enable it in nuxt.config.ts
:
export default defineNuxtConfig({
modules: ['@nuxtjs/axios'],
axios: {
baseURL: process.env.NUXT_PUBLIC_API_BASE_URL
}
})
Use it in your component:
const posts = await useNuxtApp().$axios.$get('/posts')
You've just created a clean full-stack app using Laravel 12 for the backend and Nuxt 3 for the frontend.
This setup is perfect for:
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google