To use Inertia.js you need both a server-side adapter (like this one) as well as a client-side adapter, such as inertia-vue. Be sure to also follow the installation instructions for the client-side adapter you choose. This documentation will only cover the Laravel adapter setup.
Install using Composer:
composer require inertiajs/inertia-laravel
Setup root template
The first step to using Inertia is creating a root template. We recommend using app.blade.php. This template should include your assets, as well as the @inertia directive.
If you'd like to use a different root view, you can change it using Inertia::setRootView().
Add Inertia middleware
Next, add the Inertia\Middleware middleware to your web middleware group, found in the /app/Http/Kernel.php file. This middleware monitors for asset changes, and also fixes an edge case with 302 redirects. Be sure to include this middleware after any session related middleware.
When making a non-GET Inertia request, via <inertia-link> or manually, be sure to still respond with a proper Inertia response. For example, if you're creating a new user, have your "store" endpoint return a redirect back to a standard GET endpoint, such as your user index page. Inertia will automatically follow this redirect and update the page accordingly. Here's a simplified example.
Note, when redirecting after a PUT, PATCH or DELETE request you must use a 303 response code, otherwise the subsequent request will not be treated as a GET request. A 303 redirect is the same as a 302 except that the follow-up request is explicitly changed to a GET request.
To share data with all your components, use Inertia::share($key, $data). This can be done both synchronously and lazily.
You can also get shared data using the same method Inertia::share($key). If the key is not found, null is returned.
Accessing data in root template
There are situations where you may want to access your prop data in your root Blade template. For example, you may want to add a meta description tag, Twitter card meta tags, or Facebook Open Graph meta tags. These props are available via the $page variable.
You can then access this variable like a regular Blade variable.
One common challenge with single-page apps is refreshing site assets when they've been changed. Inertia makes this easy by optionally tracking the current version of your site assets. In the event that an asset changes, Inertia will automatically make a hard page visit instead of a normal ajax visit on the next request.
To enable automatic asset refreshing, first call the Inertia::version($version) method with your current asset version. We recommend putting this in a service provider.
If you're using Laravel Mix, you can use the mix-manifest.json for this. Here's an example of that using lazy evaluation.