laravel new inertia
cd inertia
composer require inertiajs/inertia-laravel
php artisan inertia:middleware
npm install vue@next
create directory resources/js/Pages
npm install @vitejs/plugin-vue
Create Welcome.vue
<template>
Hello World
</template>
<script>
export default {};
</script>
To make shared component
Shared data
Global Component
createApp({ render: () => h(App, props) })
.use(plugin)
.component('Link', Link)
.mount(el)
Persistent Layout
export default {
components : { Layout }
};
// to
export default {
layout: Layout
};
Default Layout
import Layout from './Shared/Layout.vue'
createInertiaApp({
resolve: name => {
const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })
let page = pages[`./Pages/${name}.vue`]
page.default.layout = page.default.layout || Layout
return page
},
// ...
})
Dynamic title and meta tags
<template>
<Head>
<title>My app - Home</title>
</Head>
<h1 class="text-3xl">Home</h1>
</template>
<script setup>
import { Head } from '@inertiajs/vue3';
</script>
OR
<template>
<Head title="App - Home" />
<h1 class="text-3xl">Home</h1>
</template>
<script setup>
import { Head } from '@inertiajs/vue3';
</script>
OR
createInertiaApp({
...
title: title => 'My App - ' + title
});
Listing users
Route::get('/users', function(){
return Inertia::render('Users', [
'users' => User::all()->map(fn($user) => [
'name' => $user->name
])
]);
});
<template>
<Head title="Users" />
<h1 class="text-3xl">Users</h1>
<ul>
<li v-for="user in users" :key="user.id" v-text="user.name"></li>
</ul>
</template>
<script setup>
defineProps({ users: Array });
</script>
Pagination
Inertia and SPA Technique
Import Aliases
I’m using Vite so this is the code
const path = require('path');
export default defineConfig({
resolve: {
alias: {
'@' : path.resolve('resources/js')
},
},
});
npx mix -p // production
mix.extract() // to extract node modules
Api Resource
php artisan make:resource UserResource
return UserResource::collection(User::all());
UserResource::collection(
User::query()
->when(\\Request::input('search'), function($query, $search) {
$query->where('name', 'like', '%'.$search.'%');
})
->paginate(10)
->withQueryString()
),
Wrap In a Module