Skip to content

Responses

The Basics

Responses

Creating Responses

Creating an Inertia response is simple. To get started, invoke Inertia::render()inertia::render() within your controller or route handler, providing both the name of the JavaScript page component that you wish to render, as well as any properties (data) for the page.

In the example below, we will pass a single property (event) which contains four attributes (id, title, start_date and description) to the Event/Show page component.

1use Inertia\Inertia;
2 
3class EventsController extends Controller
4{
5 public function show(Event $event)
6 {
7 return Inertia::render('Event/Show', [
8 'event' => $event->only(
9 'id',
10 'title',
11 'start_date',
12 'description'
13 ),
14 ]);
15 
16 // Alternatively, you can use the inertia() helper...
17 return inertia('Event/Show', [
18 'event' => $event->only(
19 'id',
20 'title',
21 'start_date',
22 'description'
23 ),
24 ]);
25 }
26}
1use Inertia\Inertia;
2 
3class EventsController extends Controller
4{
5 public function show(Event $event)
6 {
7 return Inertia::render('Event/Show', [
8 'event' => $event->only(
9 'id',
10 'title',
11 'start_date',
12 'description'
13 ),
14 ]);
15 
16 // Alternatively, you can use the inertia() helper...
17 return inertia('Event/Show', [
18 'event' => $event->only(
19 'id',
20 'title',
21 'start_date',
22 'description'
23 ),
24 ]);
25 }
26}
1use laravel::inertia::{self, Page, props};
2use serde_json::json;
3 
4use crate::models::Event;
5 
6pub async fn show(event: Event) -> Page {
7 inertia::render(
8 "Event/Show",
9 props! {
10 event => json!({
11 "id": event.id,
12 "title": event.title,
13 "start_date": event.start_date,
14 "description": event.description,
15 }),
16 },
17 )
18}
1use laravel::inertia::{self, Page, props};
2use serde_json::json;
3 
4use crate::models::Event;
5 
6pub async fn show(event: Event) -> Page {
7 inertia::render(
8 "Event/Show",
9 props! {
10 event => json!({
11 "id": event.id,
12 "title": event.title,
13 "start_date": event.start_date,
14 "description": event.description,
15 }),
16 },
17 )
18}

The component name may also be a Backed Enum, which is useful for organizing page components with type-safe references.

1enum Page: string
2{
3 case EventShow = 'Event/Show';
4 case EventIndex = 'Event/Index';
5}
6 
7return Inertia::render(Page::EventShow, [
8 'event' => $event,
9]);
1enum Page: string
2{
3 case EventShow = 'Event/Show';
4 case EventIndex = 'Event/Index';
5}
6 
7return Inertia::render(Page::EventShow, [
8 'event' => $event,
9]);

To ensure that pages load quickly, only return the minimum data required for the page.

Be aware that all data returned from your controllers or handlers will be visible client-side, so be sure to omit sensitive information.

Properties

To pass data from the server to your page components, you can use properties. You can pass various types of values as props, including primitive types, collections, objects, and framework-specific types that are automatically resolved:

1use App\Models\User;
2use Illuminate\Http\Resources\Json\JsonResource;
3 
4Inertia::render('Dashboard', [
5 // Primitive values
6 'title' => 'Dashboard',
7 'count' => 42,
8 'active' => true,
9 
10 // Arrays and objects
11 'settings' => ['theme' => 'dark', 'notifications' => true],
12 
13 // Arrayable objects (Collections, Models, etc.)
14 'user' => auth()->user(), // Eloquent model
15 'users' => User::all(), // Eloquent collection
16 
17 // API Resources
18 'profile' => new UserResource(auth()->user()),
19 
20 // Responsable objects
21 'data' => new JsonResponse(['key' => 'value']),
22 
23 // Closures
24 'timestamp' => fn() => now()->timestamp,
25]);
1use App\Models\User;
2use Illuminate\Http\Resources\Json\JsonResource;
3 
4Inertia::render('Dashboard', [
5 // Primitive values
6 'title' => 'Dashboard',
7 'count' => 42,
8 'active' => true,
9 
10 // Arrays and objects
11 'settings' => ['theme' => 'dark', 'notifications' => true],
12 
13 // Arrayable objects (Collections, Models, etc.)
14 'user' => auth()->user(), // Eloquent model
15 'users' => User::all(), // Eloquent collection
16 
17 // API Resources
18 'profile' => new UserResource(auth()->user()),
19 
20 // Responsable objects
21 'data' => new JsonResponse(['key' => 'value']),
22 
23 // Closures
24 'timestamp' => fn() => now()->timestamp,
25]);
1use laravel::inertia::{self, Page, props};
2use laravel::prelude::*;
3use serde_json::json;
4 
5use crate::models::User;
6 
7pub async fn index(request: Request) -> laravel::Result<Page> {
8 let user: Option<User> = request.user().await?;
9 let users = User::all().await?;
10 
11 Ok(inertia::render("Dashboard", props! {
12 // Primitive values
13 title => "Dashboard",
14 count => 42,
15 active => true,
16 
17 // Nested JSON objects
18 settings => json!({ "theme": "dark", "notifications": true }),
19 
20 // Models and lists (any `Serialize` type)
21 user,
22 users,
23 
24 // Lazy closures
25 all_users => inertia::lazy(async || User::all().await),
26 }))
27}
1use laravel::inertia::{self, Page, props};
2use laravel::prelude::*;
3use serde_json::json;
4 
5use crate::models::User;
6 
7pub async fn index(request: Request) -> laravel::Result<Page> {
8 let user: Option<User> = request.user().await?;
9 let users = User::all().await?;
10 
11 Ok(inertia::render("Dashboard", props! {
12 // Primitive values
13 title => "Dashboard",
14 count => 42,
15 active => true,
16 
17 // Nested JSON objects
18 settings => json!({ "theme": "dark", "notifications": true }),
19 
20 // Models and lists (any `Serialize` type)
21 user,
22 users,
23 
24 // Lazy closures
25 all_users => inertia::lazy(async || User::all().await),
26 }))
27}

Arrayable objects like Eloquent models and collections are automatically converted using their toArray() method, and Responsable objects like API resources and JSON responses are resolved through their toResponse() method.

Any Serialize value can be passed to props!, and private model fields marked #[serde(skip)] are automatically omitted.

Integers outside JavaScript's safe range lose precision in the browser. You may preserve them as native BigInt values using big integer support.

ProvidesInertiaProperty Interface

In Laravel PHP, when passing props to your components, you may want to create custom classes that can transform themselves into the appropriate data format. While Laravel's Arrayable interface simply converts objects to arrays, Inertia offers the more powerful ProvidesInertiaProperty interface for context-aware transformations.

This interface requires a toInertiaProperty method that receives a PropertyContext object containing the property key ($context->key), all props for the page ($context->props), and the request instance ($context->request).

1use Inertia\PropertyContext;
2use Inertia\ProvidesInertiaProperty;
3 
4class UserAvatar implements ProvidesInertiaProperty
5{
6 public function __construct(protected User $user, protected int $size = 64)
7 {
8 //
9 }
10 
11 public function toInertiaProperty(PropertyContext $context): mixed
12 {
13 return $this->user->avatar
14 ? Storage::url($this->user->avatar)
15 : "https://ui-avatars.com/api/?name={$this->user->name}&size={$this->size}";
16 }
17}
1use Inertia\PropertyContext;
2use Inertia\ProvidesInertiaProperty;
3 
4class UserAvatar implements ProvidesInertiaProperty
5{
6 public function __construct(protected User $user, protected int $size = 64)
7 {
8 //
9 }
10 
11 public function toInertiaProperty(PropertyContext $context): mixed
12 {
13 return $this->user->avatar
14 ? Storage::url($this->user->avatar)
15 : "https://ui-avatars.com/api/?name={$this->user->name}&size={$this->size}";
16 }
17}

Once defined, you can use this class directly as a prop value.

1Inertia::render('Profile', [
2 'user' => $user,
3 'avatar' => new UserAvatar($user, 128),
4]);
1Inertia::render('Profile', [
2 'user' => $user,
3 'avatar' => new UserAvatar($user, 128),
4]);

The PropertyContext gives you access to the property key, which enables powerful patterns like merging with shared data.

1use Inertia\Inertia;
2use Inertia\PropertyContext;
3use Inertia\ProvidesInertiaProperty;
4 
5class MergeWithShared implements ProvidesInertiaProperty
6{
7 public function __construct(protected array $items = [])
8 {
9 //
10 }
11 
12 public function toInertiaProperty(PropertyContext $context): mixed
13 {
14 // Access the property key to get shared data
15 $shared = Inertia::getShared($context->key, []);
16 
17 // Merge with the new items
18 return array_merge($shared, $this->items);
19 }
20}
21 
22// Usage
23Inertia::share('notifications', ['Welcome back!']);
24 
25return Inertia::render('Dashboard', [
26 'notifications' => new MergeWithShared(['New message received']),
27 // Result: ['Welcome back!', 'New message received']
28]);
1use Inertia\Inertia;
2use Inertia\PropertyContext;
3use Inertia\ProvidesInertiaProperty;
4 
5class MergeWithShared implements ProvidesInertiaProperty
6{
7 public function __construct(protected array $items = [])
8 {
9 //
10 }
11 
12 public function toInertiaProperty(PropertyContext $context): mixed
13 {
14 // Access the property key to get shared data
15 $shared = Inertia::getShared($context->key, []);
16 
17 // Merge with the new items
18 return array_merge($shared, $this->items);
19 }
20}
21 
22// Usage
23Inertia::share('notifications', ['Welcome back!']);
24 
25return Inertia::render('Dashboard', [
26 'notifications' => new MergeWithShared(['New message received']),
27 // Result: ['Welcome back!', 'New message received']
28]);

ProvidesInertiaProperties Interface

You may want to group related props together for reusability across different pages. You can accomplish this by implementing the ProvidesInertiaProperties interface.

This interface requires a toInertiaProperties method that returns an array of key-value pairs. The method receives a RenderContext object containing the component name ($context->component) and request instance ($context->request).

1use App\Models\User;
2use Illuminate\Container\Attributes\CurrentUser;
3use Inertia\RenderContext;
4use Inertia\ProvidesInertiaProperties;
5 
6class UserPermissions implements ProvidesInertiaProperties
7{
8 public function __construct(#[CurrentUser] protected User $user)
9 {
10 //
11 }
12 
13 public function toInertiaProperties(RenderContext $context): array
14 {
15 return [
16 'canEdit' => $this->user->can('edit'),
17 'canDelete' => $this->user->can('delete'),
18 'canPublish' => $this->user->can('publish'),
19 'isAdmin' => $this->user->hasRole('admin'),
20 ];
21 }
22}
1use App\Models\User;
2use Illuminate\Container\Attributes\CurrentUser;
3use Inertia\RenderContext;
4use Inertia\ProvidesInertiaProperties;
5 
6class UserPermissions implements ProvidesInertiaProperties
7{
8 public function __construct(#[CurrentUser] protected User $user)
9 {
10 //
11 }
12 
13 public function toInertiaProperties(RenderContext $context): array
14 {
15 return [
16 'canEdit' => $this->user->can('edit'),
17 'canDelete' => $this->user->can('delete'),
18 'canPublish' => $this->user->can('publish'),
19 'isAdmin' => $this->user->hasRole('admin'),
20 ];
21 }
22}

You can use these prop classes directly in the render() and with() methods.

1public function index(UserPermissions $permissions)
2{
3 return Inertia::render('UserProfile', $permissions);
4 
5 // or...
6 
7 return Inertia::render('UserProfile')->with($permissions);
8}
1public function index(UserPermissions $permissions)
2{
3 return Inertia::render('UserProfile', $permissions);
4 
5 // or...
6 
7 return Inertia::render('UserProfile')->with($permissions);
8}

You can also combine multiple prop classes with other props in an array:

1public function index(UserPermissions $permissions)
2{
3 return Inertia::render('UserProfile', [
4 'user' => auth()->user(),
5 $permissions,
6 ]);
7 
8 // or using method chaining...
9 
10 return Inertia::render('UserProfile')
11 ->with('user', auth()->user())
12 ->with($permissions);
13}
1public function index(UserPermissions $permissions)
2{
3 return Inertia::render('UserProfile', [
4 'user' => auth()->user(),
5 $permissions,
6 ]);
7 
8 // or using method chaining...
9 
10 return Inertia::render('UserProfile')
11 ->with('user', auth()->user())
12 ->with($permissions);
13}

You can group typed props into any #[derive(Serialize)] struct (or implement IntoProps) and pass it directly to inertia::render():

1#[derive(Serialize)]
2struct ShowProps {
3 user: User,
4 can_edit: bool,
5}
6 
7inertia::render("Users/Show", ShowProps { user, can_edit: true })
1#[derive(Serialize)]
2struct ShowProps {
3 user: User,
4 can_edit: bool,
5}
6 
7inertia::render("Users/Show", ShowProps { user, can_edit: true })

Root Template Data

There are situations where you may want to access your prop data in your application's root template. For example, you may want to add a meta description tag, Twitter card meta tags, or Facebook Open Graph meta tags. You can access this data via the $page variable in Bladepage variable in MiniJinja.

1<meta name="twitter:title" content="{{ $page['props']['event']->title }}">
1<meta name="twitter:title" content="{{ $page['props']['event']->title }}">
1<meta name="twitter:title" content="{{ page.props.event.title }}">
1<meta name="twitter:title" content="{{ page.props.event.title }}">

Sometimes you may even want to provide data to the root template that will not be sent to your JavaScript page component. This can be accomplished by invoking withViewDatawith_view_data.

1return Inertia::render('Event', ['event' => $event])
2 ->withViewData(['meta' => $event->meta]);
1return Inertia::render('Event', ['event' => $event])
2 ->withViewData(['meta' => $event->meta]);
1inertia::render("Event", props! { event })
2 .with_view_data("meta", &event.meta)
1inertia::render("Event", props! { event })
2 .with_view_data("meta", &event.meta)

After invoking withViewDatawith_view_data, you can access the defined data as you would typically access a template variable.

1<meta name="description" content="{{ $meta }}">
1<meta name="description" content="{{ $meta }}">
1<meta name="description" content="{{ meta }}">
1<meta name="description" content="{{ meta }}">

Maximum Response Size

To enable client-side history navigation, all Inertia server responses are stored in the browser's history state. However, keep in mind that some browsers impose a size limit on how much data can be saved within the history state.

For example, Firefox has a size limit of 16 MiB and throws a NS_ERROR_ILLEGAL_VALUE error if you exceed this limit. Typically, this is much more data than you'll ever practically need when building applications.