---
title: "Building Modern Single-Page Applications with Laravel and Inertia.js"
url: "https://magecomp.com/blog/building-modern-single-page-applications-with-laravel-and-inertia-js/"
date: "2024-10-09T15:02:59+00:00"
modified: "2025-01-01T12:49:39+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "Laravel"
word_count: 1021
reading_time: "6 min read"
summary: "In today's web development landscape, developers often seek efficient ways to build dynamic, responsive applications without the overhead of managing complex APIs or client-side routing. Inertia.js..."
description: "Learn how to build modern single-page applications (SPAs) using Laravel and Inertia.js."
keywords: "Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Create and Register Middleware in Laravel 11?"
    url: "https://magecomp.com/blog/create-register-middleware-laravel-11/"
  - title: "How to Integrate and Use MongoDB with Laravel?"
    url: "https://magecomp.com/blog/integrate-mongodb-with-laravel/"
  - title: "Laravel 12: Add or Remove Multiple Input Fields with jQuery Example"
    url: "https://magecomp.com/blog/laravel-12-add-remove-multiple-input-fields-with-jquery/"
---

# Building Modern Single-Page Applications with Laravel and Inertia.js

_Published: October 9, 2024_  
_Author: Bharat Desai_  

![Building Modern Single-Page Applications with Laravel and Inertia.js](https://magecomp.com/blog/wp-content/uploads/2024/10/Building-Modern-Single-Page-Applications-with-Laravel-and-Inertia.js-jpeg-1024x512.avif)

In today’s web development landscape, developers often seek efficient ways to build dynamic, responsive applications without the overhead of managing complex APIs or client-side routing. Inertia.js, in combination with Laravel, offers an innovative approach to building modern Single-Page Applications (SPAs) while maintaining a simple and familiar development experience. This blog will introduce Inertia.js and demonstrate how it integrates with Laravel through a step-by-step example.
## [![Laravel Development Services](https://magecomp.com/blog/wp-content/uploads/2024/12/Laravel-Development-Services-4.webp)](https://magecomp.com/services/laravel-development-services/)

## What is Inertia.js?
Inertia.js provides a unique way to build SPAs without requiring a separate API for your frontend. Unlike traditional SPAs that rely on client-side frameworks like React or Vue for routing and state management, Inertia leverages server-side routing with Laravel. It bridges the gap between server-side applications and SPAs, allowing you to keep the backend logic while delivering dynamic, seamless frontend experiences.
## Why Use Inertia.js with Laravel?
- **Eliminate the API Layer:** You don’t need to build and maintain a RESTful API or GraphQL layer.- **Keep Laravel Routing:** Inertia lets you use Laravel’s server-side routing without the need for frontend routing libraries.- **Simplified State Management:** Inertia handles data fetching and page state management, letting you focus on application logic.- **Seamless Integration:** Inertia allows developers to use popular JavaScript frameworks (like Vue or React) while still keeping the development flow in Laravel.
## Steps to Build Single-Page Applications with Laravel and Inertia.js:
### Prerequisites
To follow along with this tutorial, you should have:
- A working Laravel application (Laravel 8+)- Familiarity with Vue.js or React (this tutorial uses Vue.js)- Node.js and npm installed
### Step 1: Installing Inertia.js and Vue.js
Start by adding the Inertia.js server-side package to your Laravel project:
```
composer require inertiajs/inertia-laravel
```

Next, install Vue.js and the necessary client-side packages for Inertia.js:
```
npm install vue@next @inertiajs/inertia @inertiajs/inertia-vue3
```

After installation, run the following command to compile your assets:
```
npm run dev
```

### Step 2: Configure Inertia and Vue.js
We’ll now configure Laravel to work with Vue.js and Inertia.js. Open **resources/js/app.js** and update it to include Inertia’s setup with Vue.js:
```
import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';

createInertiaApp({
  resolve: name => require(`./Pages/${name}`),
  setup({ el, App, props, plugin }) {
    createApp({ render: () => h(App, props) })
      .use(plugin)
      .mount(el);
  },
});
```

Next, modify the Laravel layout file **resources/views/app.blade.php** to include Inertia:
```

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title inertia>{{ config('app.name', 'Laravel') }}</title>
    @vite('resources/js/app.js')
    @inertiaHead
</head>
<body>
    @inertia
</body>
</html>
```

This layout serves as the base HTML for every page, and @inertia renders the page content.
### [![Hire laravel Developer](https://magecomp.com/blog/wp-content/uploads/2024/12/Hire-Laravel-Expert-Now-3.webp)](https://magecomp.com/services/hire-laravel-developer/)

### Step 3: Building Your First Inertia.js Page
Let’s build a basic example to showcase a User List using Laravel’s controllers and Inertia.js’s Vue components.
**Controller Setup**

Create a new controller using the following command:
```
php artisan make:controller UserController
```

Next, update the UserController to fetch user data and pass it to the Vue component via Inertia:
```
namespace AppHttpControllers;

use AppModelsUser;
use InertiaInertia;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();
        return Inertia::render('Users/Index', [
            'users' => $users,
        ]);
    }
}
```

**Defining the Route**

Now, let’s define a route in routes/web.php to display the user list:
```
Route::get('/users', [UserController::class, 'index']);
```

**Creating the Vue Component**

In the resources/js/Pages/Users folder, create a Index.vue file to display the list of users:
```
<template>
      <h1>User List</h1>
                  {{ user.name }}
            </template>

<script>
export default {
  props: {
    users: Array,
  },
}
</script>
```

This component receives a list of users from Laravel and displays it. When you visit /users in your browser, the page will load without a full refresh, thanks to Inertia’s SPA capabilities.
### Step 4: Handling Form Submissions with Inertia.js
One of the advantages of Inertia is that it allows you to handle form submissions without having to reload the entire page. Let’s demonstrate this by creating a simple form to add a new user.
**Creating a Form Page**

First, create a new route in web.php for the form and submission:
```
Route::get('/users/create', [UserController::class, 'create']);
Route::post('/users', [UserController::class, 'store']);
```

In UserController.php, add the methods to display and handle the form submission:
```
public function create()
{
    return Inertia::render('Users/Create');
}

public function store(Request $request)
{
    $request->validate([
        'name' => 'required|string|max:255',
    ]);

    User::create([
        'name' => $request->name,
    ]);

    return redirect('/users');
}
```

**Creating the Form Component**

Now, create a Create.vue component for the form in resources/js/Pages/Users/Create.vue:
```
<template>
  <form @submit.prevent="submit">
          <label for="name">Name:</label>
      <input v-model="form.name" type="text" id="name">
        <button type="submit">Add User</button>
  </form>
</template>

<script>
import { Inertia } from '@inertiajs/inertia';

export default {
  data() {
    return {
      form: {
        name: '',
      },
    };
  },
  methods: {
    submit() {
      Inertia.post('/users', this.form);
    },
  },
}
</script>
```

This form uses Vue’s v-model to bind input values and uses Inertia to submit the form data without reloading the page.
## Conclusion
Inertia.js offers a streamlined way to build SPAs using Laravel’s familiar server-side routing and data handling. By leveraging Inertia, developers can create highly dynamic, modern applications with minimal overhead and complexity, all while keeping the development flow intuitive and straightforward.
[![Contact Us](https://magecomp.com/blog/wp-content/uploads/2024/12/Contact-Us-3.webp)](https://magecomp.com/contact)

In this tutorial, we walked through setting up Inertia.js with Laravel, creating dynamic pages, and handling form submissions, all while maintaining the SPA experience. With Inertia.js, developers can focus more on building features and less on managing the complexities of API endpoints and client-side routing.
</body></html>


---

_View the original post at: [https://magecomp.com/blog/building-modern-single-page-applications-with-laravel-and-inertia-js/](https://magecomp.com/blog/building-modern-single-page-applications-with-laravel-and-inertia-js/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-02 15:47:38 UTC_  
