This course is still the 0.x beta. The starter renders pages with Inertia, React, TypeScript, Tailwind, and Vite. Inertia is not a second router. A Laravel handler returns the component name and the props. The component lives in resources/js/pages.
Inertia only runs on routes that pass through HandleInertiaRequests. New applications append that middleware to the web group in src/app.rs. Calling inertia::render on an api route returns a server error. Keep the notebook in web.rs.
The index
use laravel::inertia::{self, Page, props};
use laravel::prelude::*;
use crate::models::Note;
pub async fn index() -> laravel::Result<Page> {
let notes = Note::query()
.with(Note::user)
.latest(Note::created_at)
.limit(20)
.get()
.await?;
Ok(inertia::render("Notes/Index", props! { notes }))
}
"Notes/Index" is resources/js/pages/Notes/Index.tsx. props! accepts a local by name. A variable listed that way is borrowed, so you can still use notes afterward. Keys keep the casing you write.
Every prop is visible to anyone who inspects the page. Send the fields the screen needs. PasswordHash and RememberToken do not implement Serialize, so a User only compiles into a page when those fields are #[serde(skip)]. The starter already skips them. If you add a secret of your own to Note, skip that field too. Inertia will not drop it for you.
The loaded author is part of notes because you called .with(Note::user). The relationship dereferences to User. An unloaded relationship panics when the page is serialized, with the same “isn’t loaded” message as in the records course.
type Note = {
id: number;
title: string;
body: string;
user: { id: number; name: string };
};
export default function Index({ notes }: { notes: Note[] }) {
return (
<ul>
{notes.map((note) => (
<li key={note.id}>{note.title} — {note.user.name}</li>
))}
</ul>
);
}
The create form is the <Form> component from the Inertia guide. It posts to your handler and expects a redirect:
import { Form } from "@inertiajs/react";
<Form action="/notes" method="post">
{({ errors, processing }) => (
<>
<input type="text" name="title" />
{errors.title && <p>{errors.title}</p>}
<textarea name="body" />
{errors.body && <p>{errors.body}</p>}
<button type="submit" disabled={processing}>Save</button>
</>
)}
</Form>
Do not put user_id on this form. StoreNote ignores it, and the handler sets user from the session. A validation failure on a web route redirects back with the errors. <Form> reads those errors. A JSON client still gets 422. That split is the validation guide, not something the React page decides.
laravel dev already runs Vite. Saving the TSX hot-reloads. Saving the Rust handler rebuilds the server. If the component name and the file path disagree, the page cannot resolve. The string and the path are the contract.
Check your work
Open /notes while signed in. The React page should list titles, and the page JSON should not contain a password hash.