Certainly! Here’s a suggested solution for handling CRUD pages with modals in Inertia.js v2 (commonly with Laravel + Vue/React/Svelte). The recommended “Inertia way” is to treat modals as part of your page state, not separate requests or views. This makes navigation (including opening modals via links) possible and works seamlessly with browser navigation.
Below is a general approach (using Vue as an example):
1. Modal State via Query Strings
When a user triggers a modal (Create/Edit/etc), update the browser URL (with Inertia.visit) to include something like ?modal=create or ?modal=edit&id=1.
2. Controller Handles Modal Data
In your controller, detect if a modal is requested (look for those query params), then pass the necessary data (e.g., record to edit) to the Inertia page props.
Example Controller:
public function index(Request $request)
{
$items = Item::all(); // Or paginate
$modal = $request->query('modal');
$modalData = null;
if ($modal === 'edit' && $request->has('id')) {
$modalData = Item::findOrFail($request->input('id'));
}
return Inertia::render('Items/Index', [
'items' => $items,
'modal' => $modal,
'modalData' => $modalData,
]);
}
3. Page Handles Modal Rendering
In your Vue component, reactively watch the modal prop and render the modal accordingly.
<template>
<div>
<ItemTable :items="items" @edit="openEdit" />
<Modal v-if="modal === 'edit'" @close="closeModal">
<EditItemForm :item="modalData" @saved="closeModal" />
</Modal>
</div>
</template>
<script>
export default {
props: {
items: Array,
modal: String,
modalData: Object,
},
methods: {
openEdit(item) {
this.$inertia.visit(route('items.index', { modal: 'edit', id: item.id }), { preserveScroll: true, preserveState: true })
},
closeModal() {
this.$inertia.visit(route('items.index'), { preserveScroll: true, preserveState: true })
}
}
}
</script>
4. No AJAX, No Extra Pages
No need for separate AJAX requests or additional page files. Everything flows through Inertia and is managed as page state.
Benefits:
- Browser back/forward just works (modals open/close as expected).
- SEO and shareable links (you can share a link to
items?modal=edit&id=1). - Keeps to “Inertia Philosophy”: server returns full page state, not partials/fragments.
Summary:
- Use query params to represent modal state.
- Handle modal data fetching server-side in your controller.
- Render modals in your client page based on Inertia props.
- Use Inertia navigation for opening/closing modals.
Let me know if you want a more detailed code example with Create/Update/Delete flows!