This is part 2 of the series “Building a Book Selling Website with Laravel and Nuxt 3” and in this part we will start connecting Laravel with Nuxt.js 3 and implement other stuff.
Table of Contents
- Preparing Project
- Connecting Laravel with Nuxt 3
- Adding Cart Functionality
- Checkout and Storing Orders
Let’s start building the frontend. We will use Nuxt’s built-in useFetch() for retrieving API data.
One advantage of useFetch() is that it works with Nuxt server-side rendering, which is useful for an e-commerce website because book and category pages should be discoverable by search engines.
Preparing Frontend
-
Create an API Composable
Create app/composables/useApi.ts:
export const useApi = () => {
const config = useRuntimeConfig()
const apiFetch = <T>(endpoint: string, options: Record<string, any>) => {
return useFetch<T>(`${config.public.apiBase}${endpoint}`, options as any)
}
return {
apiFetch
}
}
This composable gives us with a reusable way to communicate with laravel.
Example usage:
const { apiFetch } = useApi()
const { data, pending, error } = await apiFetch('/books')
We won’t need to repeat the API base URL in every page.
-
Create the main layout
Create app/layouts/default.vue:
<template>
<div>
<header class="bg-white shadow-sm">
<nav class="container mx-auto px-4 py-5 flex justify-between">
<NuxtLink to="/" class="text-2xl font-bold text-indigo-700">
BookStore
</NuxtLink>
<div class="flex gap-6">
<NuxtLink to="/">Home</NuxtLink>
<NuxtLink to="/books">Books</NuxtLink>
</div>
</nav>
</header>
<main class="container mx-auto px-4 py-8">
<slot />
</main>
</div>
</template>
Next enable to default layout by opening app/app.vue and replacing it with this code:
app/app.vue
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
You can learn more about layouts on Nuxt docs.
Launch the Nuxt development server:
npm run dev
If you navigated to http://localhost:3000 in your browser you will see the new layout, but without styling.
For styling, let’s use tailwindcss.
Install it using the Nuxt module:
npx nuxi@latest module add tailwindcss
This will configure the Tailwind module for our Nuxt application.Â
You will see in the nuxt.config.ts a new entry added in the modules:
export default defineNuxtConfig({
modules: ['@nuxtjs/tailwindcss'],
...
..
});
-
Create a reusable Book Card component
Create components/BookCard.vue
<script setup lang="ts">
import type {Book} from "~/inc/types.ts";
defineProps<{ book: Book }>()
</script>
<template>
<NuxtLink
:to="`/books/${book.slug}`"
class="block bg-white rounded-lg shadow-sm overflow-hidden
hover:shadow-lg transition"
>
<div class="aspect-[3/4] bg-gray-100">
<img
v-if="book.cover_image"
:src="book.cover_image"
:alt="book.title"
class="w-full h-full object-cover"
/>
<div
v-else
class="w-full h-full flex items-center justify-center text-gray-400"
>
No Cover
</div>
</div>
<div class="p-4">
<p class="text-sm text-indigo-600 mb-2">
{{ book.category?.name }}
</p>
<h3 class="font-semibold text-lg mb-1">
{{ book.title }}
</h3>
<p class="text-sm text-gray-500 mb-3">
{{ book.author }}
</p>
<p class="font-bold text-indigo-700">
${{ book.price }}
</p>
</div>
</NuxtLink>
</template>
The component receives a book object as a prop and displays its information.
We can reuse this component on the homepage, category pages, and search results.
Create the typescript types for Book and Category in app/inc/types.ts:
export type Category = {
id?: number
name: string
slug: string
}
export type Book = {
id: number
title: string
slug: string
author: string
price: string
cover_image: string | null
category: Category
}
-
Building the Nuxt storefront
We will need these pages:
- Homepage: Displays featured books and categories.
- Books listing: Displays all books with search and category filtering.
- Book details: Displays the selected book’s information.
Creating the homepage
Create pages/index.vue
<script setup lang="ts">
const { apiFetch } = useApi()
const { data: booksResponse, pending, error } = await apiFetch<{ data: any[] }>('/books')
const { data: categoriesResponse } = await apiFetch<{ data: any[] }>('/categories')
const books = computed(() => booksResponse.value?.data ?? [])
const categories = computed(() => categoriesResponse.value?.data ?? [])
</script>
<template>
<div>
<!-- Hero Section -->
<section class="bg-indigo-700 text-white rounded-xl p-10 mb-10">
<h1 class="text-4xl font-bold mb-4">
Discover Your Next Favorite Book
</h1>
<p class="text-indigo-100 mb-6">
Explore our collection of books and find something inspiring.
</p>
<NuxtLink
to="/books"
class="inline-block bg-white text-indigo-700 px-6 py-3 rounded-lg font-semibold"
>
Browse Books
</NuxtLink>
</section>
<!-- Categories -->
<section class="mb-10">
<h2 class="text-2xl font-bold mb-5">Categories</h2>
<div class="flex flex-wrap gap-3">
<NuxtLink
v-for="category in categories"
:key="category.id"
:to="`/books?category=${category.slug}`"
class="bg-gray-100 hover:bg-indigo-100 px-5 py-3 rounded-lg"
>
{{ category.name }}
</NuxtLink>
</div>
</section>
<!-- Featured Books -->
<section>
<div class="flex justify-between items-center mb-5">
<h2 class="text-2xl font-bold">Latest Books</h2>
<NuxtLink to="/books" class="text-indigo-700">
View All
</NuxtLink>
</div>
<div v-if="pending" class="py-10 text-center">
Loading books...
</div>
<div v-else-if="error" class="py-10 text-center text-red-600">
Unable to load books.
</div>
<div
v-else
class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6"
>
<BookCard
v-for="book in books.slice(0, 8)"
:key="book.id"
:book="book"
/>
</div>
</section>
</div>
</template>
Notice that we are requesting both books and categories from Laravel.
We also reuse the BookCard component instead of repeating the book presentation markup.
You can view the updated home page but first make sure the laravel dev server is running:
php artisan serve
Creating the books listing page
Create pages/books/index.vue
This page will support:
– Displaying all books.
– Searching by title or author.
– Filtering by category.
<script setup lang="ts">
const route = useRoute()
const router = useRouter()
const { apiFetch } = useApi()
const search = ref(
typeof route.query.search === 'string'
? route.query.search
: ''
)
const category = ref(
typeof route.query.category === 'string'
? route.query.category
: ''
)
const page = computed(() => Number(route.query.page) || 1)
const { data: categoriesResponse } =
await apiFetch<{ data: any[] }>('/categories')
const categories = computed(
() => categoriesResponse.value?.data ?? []
)
const { data: booksResponse, pending, error } = await useFetch<{
data: any[]
meta: {
current_page: number
last_page: number
}
}>('/books', {
baseURL: useRuntimeConfig().public.apiBase,
query: computed(() => ({
search: route.query.search,
category: route.query.category,
page: page.value
})),
watch: [() => route.query]
})
const books = computed(() => booksResponse.value?.data ?? [])
const pagination = computed(
() => booksResponse.value?.meta
)
function applyFilters() {
router.push({
path: '/books',
query: {
...(search.value ? { search: search.value } : {}),
...(category.value ? { category: category.value } : {})
}
})
}
function changePage(nextPage: number) {
router.push({
path: '/books',
query: {
...route.query,
page: nextPage
}
})
}
</script>
<template>
<div>
<h1 class="text-3xl font-bold mb-6">Our Books</h1>
<!-- Filters -->
<div class="flex flex-col sm:flex-row gap-3 mb-4">
<input
v-model="search"
type="text"
placeholder="Search books or authors..."
class="border rounded-lg px-4 py-3 flex-1"
@keyup.enter="applyFilters"
/>
<select
v-model="category"
class="border rounded-lg px-4 py-3"
>
<option value="">All Categories</option>
<option
v-for="item in categories"
:key="item.id"
:value="item.slug"
>
{{ item.name }}
</option>
</select>
<button
@click="applyFilters"
class="bg-indigo-700 text-white rounded-lg px-6 py-3"
>
Search
</button>
</div>
<!-- Results -->
<div v-if="pending" class="py-10 text-center">
Loading books...
</div>
<div v-else-if="error" class="py-10 text-center text-red-600">
Unable to load books.
</div>
<template v-else>
<div
v-if="books.length"
class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6"
>
<BookCard
v-for="book in books"
:key="book.id"
:book="book"
/>
</div>
<p v-else class="text-center text-gray-500 py-10">
No books found.
</p>
<!-- Pagination -->
<div
v-if="pagination && pagination.last_page > 1"
class="flex justify-center items-center gap-4 mt-10"
>
<button
:disabled="pagination.current_page <= 1"
@click="changePage(pagination.current_page - 1)"
class="border rounded px-4 py-2 disabled:opacity-40"
>
Previous
</button>
<span>
Page {{ pagination.current_page }}
of {{ pagination.last_page }}
</span>
<button
:disabled="pagination.current_page >= pagination.last_page"
@click="changePage(pagination.current_page + 1)"
class="border rounded px-4 py-2 disabled:opacity-40"
>
Next
</button>
</div>
</template>
</div>
</template>
An important detail here is that we use the URL query parameters to maintain the current filters.
For example:
/books?search=clean /books?category=programming /books?search=clean&category=programming /books?page=2
Creating the book details page
Create:
pages/books/[slug].vue
Nuxt uses the dynamic [slug] filename to create a page for each book.
<script setup lang="ts">
const route = useRoute()
const config = useRuntimeConfig()
const slug = computed(() => String(route.params.slug))
const { data: response, pending, error } = await useFetch<{data: any}>(() => `${config.public.apiBase}/books/${slug.value}`)
const book = computed(() => response.value?.data)
</script>
<template>
<div>
<div v-if="pending" class="text-center py-10">
Loading book details...
</div>
<div v-else-if="error" class="text-center py-10">
Book not found.
</div>
<div
v-else-if="book"
class="grid grid-cols-2 md:grid-cols-1 gap-2"
>
<!-- Book Cover -->
<div class="bg-gray-100 rounded-xl overflow-hidden aspect-[3/4] max-w-md">
<img
v-if="book.cover_image"
:src="book.cover_image"
:alt="book.title"
class="w-full h-full object-cover"
/>
<div
v-else
class="h-full flex items-center justify-center text-gray-400"
>
No Cover Available
</div>
</div>
<!-- Book Information -->
<div>
<NuxtLink
:to="`/books?category=${book.category.slug}`"
class="text-indigo-700 mb-4 inline-block"
>
{{ book.category.name }}
</NuxtLink>
<h1 class="text-3xl font-bold mb-3">
{{ book.title }}
</h1>
<p class="text-gray-500 mb-6">
By {{ book.author }}
</p>
<p class="text-2xl font-bold text-indigo-700 mb-6">
${{ book.price }}
</p>
<p class="text-gray-600 leading-7 mb-6">
{{ book.description }}
</p>
<p class="mb-6">
<span v-if="book.stock > 0" class="text-green-600">
In Stock
</span>
<span v-else class="text-red-600">
Out of Stock
</span>
</p>
<button
disabled
class="bg-indigo-700 px-2 text-white px-8 py-3 rounded-lg opacity-50 cursor-not-allowed"
>
Add to Cart
</button>
</div>
</div>
</div>
</template>
Here the page is composed of two columns using tailwindcss grid-cols-* classes.Â
You may encounter situation like the Nuxt can’t recognize the tailwind grid-cols-* classes and therefore the two columns appear vertically, this issue can be fixed by override tailwind configuration:
Create this file:
app/assets/css/tailwind.css
@tailwind base; @tailwind components; @tailwind utilities;
Next add the css config for the tailwind.css in nuxt.config:
nuxt.config.ts
export default defineNuxtConfig({
...
...,
css: ['~/assets/css/tailwind.css'],
})
Restart the dev server and preview the details page again it should work.
At this step you can view the homepage, the books page and book detail pages.

Continue to part 3: Shopping Cart and Order Management

