Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Starter Kits  ›  Lesson

Inertia — Laravel Routes, JavaScript Pages

Inertia 12 minInertia lets a Laravel controller render a React or Vue page component with props, so you build a single-page app without designing a separate API.

Inertia — Laravel Routes, JavaScript Pages

Some teams love React or Vue for building screens, but do not want to design, version and document a whole JSON API just to feed their own website. Inertia is the bridge. Laravel keeps routing and controllers. React or Vue draws the page. The data passes straight between them.

The rule

The controller names a page component and passes props; Inertia does the rest.

In a Blade app a controller returns view(...). In an Inertia app it returns Inertia::render(...):

use Inertia\Inertia;

Route::get('/books', function () {
    return Inertia::render('books/index', [
        'books' => Book::all(),
    ]);
});

'books/index' names a component file, such as resources/js/pages/books/index.tsx in the React kit. The array becomes that component's props:

export default function Index({ books }) {
    return <ul>{books.map(b => <li key={b.id}>{b.title}</li>)}</ul>;
}

What happens on the wire:

  1. First visit: Laravel returns a full HTML page with the JavaScript app and the first page's data embedded in it.
  2. Later clicks: Inertia's <Link> sends a background request with an X-Inertia header. Laravel runs the same route and controller, but replies with JSON: the component name, its props and the URL.
  3. The client swaps in the new component. No full reload, and it feels like a single-page app.

Forms work the same way. You post to a normal Laravel route. If validation fails, the errors come back as props, and the form shows them without losing what the member typed.

The shortcut

Read any Inertia controller as "which page, with which data?" The first argument is the page, the array is the data. Everything between server and screen is Inertia's job, not yours.

Showing one book with InertiaStep 1 of 4
  1. Route

    Route::get('/books/{book}', ...) stays pure Laravel.

The trap is writing fetch('/api/books') inside an Inertia page out of habit. The controller already sent the data as props.

Drills

Answer fast — trust the rule.

quick drill

In Inertia::render('books/index', ['books' => $books]), what is 'books/index'?

quick drill

After the first page load, what does Laravel send back for an Inertia link click?

quick drill

Which part of an Inertia app still belongs to Laravel?

quick drill

A form submitted through Inertia fails validation. What does the page receive?

Remember

  • The controller names a page component and passes props; Inertia does the rest.
  • Read Inertia::render as "which page, with which data?"
  • Trap: no hand-written API calls for your own pages. The props are already there.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFollow an Inertia click

In an Inertia version of Lendly, a member is on the books list and clicks a link to /books/7. Explain what the browser sends, what Laravel sends back, and why the page does not fully reload.