Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeroenvanrensen's avatar

Setup Ziggy with Inertia and Vue

Hello,

I'm using Laravel and I want to setup Ziggy with Inertia and Vue.

I installed the Ziggy composer package and added @routes to my layout file.

The Inertia documentation says I should add it to the Vue instance:

Vue.prototype.$route = route

However, I don't understand exactly to put it.

This is my app.js file:

import { createApp, h } from 'vue'
import { createInertiaApp, Link } from '@inertiajs/inertia-vue3'

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

Hope anyone can help me out.

Thanks!

0 likes
12 replies
Sinnbeck's avatar

@jeroenvanrensen sorry I don't use vue. Just read it at that post. I think the guide in the other answer is a good idea if you just install the npm package

1 like
jeroenvanrensen's avatar

@Nakov hello, I followed the exact steps but I got this error:

Module not found: Error: Can't resolve 'ziggy'
1 like
CamKem's avatar

Has anyone has any luck implementing this? I installed inertia & ziggy - now my app.js looks like this but still does not function correctly.

import './bootstrap';
import '../css/app.css';

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(ZiggyVue, Ziggy)
            .mount(el);
    },
});

InertiaProgress.init({ color: '#4B5563' });

I have screenshots of my dev console if I can post screenshots?

1 like
Sinnbeck's avatar

@CamKem It is generally better to create your own thread. If it takes 10 answers to fix your problem, we are spamming the original author with 10 emails, when their problem was already solved. Secondly, most people will ignore solved threads.

2 likes
YobertyAlej's avatar

@CamKem The problem is that you need to run a command in order to generate a file called ziggy.js

php artisan ziggy:generate

this file needs to be imported in the app.js of the inertia app.

import './bootstrap';
import '../css/app.css';

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';
import { Ziggy } from './ziggy'

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(ZiggyVue, Ziggy)
            .mount(el);
    },
});

InertiaProgress.init({ color: '#4B5563' });

So you basically need to generate this file anytime you register a new route, you can automate this process using vite by adding the following piece of code to the vite.config.js file

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
        {
            name: 'ziggy',
            enforce: 'post',
            handleHotUpdate({ server, file }) {
                if (file.includes('/routes/') && file.endsWith('.php')) {
                    exec('php artisan ziggy:generate', (error, stdout) => error === null && console.log(`  > Ziggy routes generated!`))
                }
        }
    },
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});

This way you will have your ziggy.js file generated automatically when runing npm run dev

2 likes

Please or to participate in this conversation.