Responses
- Creating Responses
- Properties
ProvidesInertiaPropertyInterfaceProvidesInertiaPropertiesInterface- Root Template Data
- Maximum Response Size
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;23class EventsController extends Controller4{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 ]);1516 // 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;23class EventsController extends Controller4{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 ]);1516 // 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;34use crate::models::Event;56pub 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;34use crate::models::Event;56pub 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: string2{3 case EventShow = 'Event/Show';4 case EventIndex = 'Event/Index';5}67return Inertia::render(Page::EventShow, [8 'event' => $event,9]);1enum Page: string2{3 case EventShow = 'Event/Show';4 case EventIndex = 'Event/Index';5}67return 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;34Inertia::render('Dashboard', [5 // Primitive values6 'title' => 'Dashboard',7 'count' => 42,8 'active' => true,910 // Arrays and objects11 'settings' => ['theme' => 'dark', 'notifications' => true],1213 // Arrayable objects (Collections, Models, etc.)14 'user' => auth()->user(), // Eloquent model15 'users' => User::all(), // Eloquent collection1617 // API Resources18 'profile' => new UserResource(auth()->user()),1920 // Responsable objects21 'data' => new JsonResponse(['key' => 'value']),2223 // Closures24 'timestamp' => fn() => now()->timestamp,25]);1use App\Models\User;2use Illuminate\Http\Resources\Json\JsonResource;34Inertia::render('Dashboard', [5 // Primitive values6 'title' => 'Dashboard',7 'count' => 42,8 'active' => true,910 // Arrays and objects11 'settings' => ['theme' => 'dark', 'notifications' => true],1213 // Arrayable objects (Collections, Models, etc.)14 'user' => auth()->user(), // Eloquent model15 'users' => User::all(), // Eloquent collection1617 // API Resources18 'profile' => new UserResource(auth()->user()),1920 // Responsable objects21 'data' => new JsonResponse(['key' => 'value']),2223 // Closures24 'timestamp' => fn() => now()->timestamp,25]);
1use laravel::inertia::{self, Page, props};2use laravel::prelude::*;3use serde_json::json;45use crate::models::User;67pub async fn index(request: Request) -> laravel::Result<Page> {8 let user: Option<User> = request.user().await?;9 let users = User::all().await?;1011 Ok(inertia::render("Dashboard", props! {12 // Primitive values13 title => "Dashboard",14 count => 42,15 active => true,1617 // Nested JSON objects18 settings => json!({ "theme": "dark", "notifications": true }),1920 // Models and lists (any `Serialize` type)21 user,22 users,2324 // Lazy closures25 all_users => inertia::lazy(async || User::all().await),26 }))27}1use laravel::inertia::{self, Page, props};2use laravel::prelude::*;3use serde_json::json;45use crate::models::User;67pub async fn index(request: Request) -> laravel::Result<Page> {8 let user: Option<User> = request.user().await?;9 let users = User::all().await?;1011 Ok(inertia::render("Dashboard", props! {12 // Primitive values13 title => "Dashboard",14 count => 42,15 active => true,1617 // Nested JSON objects18 settings => json!({ "theme": "dark", "notifications": true }),1920 // Models and lists (any `Serialize` type)21 user,22 users,2324 // Lazy closures25 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;34class UserAvatar implements ProvidesInertiaProperty5{6 public function __construct(protected User $user, protected int $size = 64)7 {8 //9 }1011 public function toInertiaProperty(PropertyContext $context): mixed12 {13 return $this->user->avatar14 ? 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;34class UserAvatar implements ProvidesInertiaProperty5{6 public function __construct(protected User $user, protected int $size = 64)7 {8 //9 }1011 public function toInertiaProperty(PropertyContext $context): mixed12 {13 return $this->user->avatar14 ? 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;45class MergeWithShared implements ProvidesInertiaProperty6{7 public function __construct(protected array $items = [])8 {9 //10 }1112 public function toInertiaProperty(PropertyContext $context): mixed13 {14 // Access the property key to get shared data15 $shared = Inertia::getShared($context->key, []);1617 // Merge with the new items18 return array_merge($shared, $this->items);19 }20}2122// Usage23Inertia::share('notifications', ['Welcome back!']);2425return 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;45class MergeWithShared implements ProvidesInertiaProperty6{7 public function __construct(protected array $items = [])8 {9 //10 }1112 public function toInertiaProperty(PropertyContext $context): mixed13 {14 // Access the property key to get shared data15 $shared = Inertia::getShared($context->key, []);1617 // Merge with the new items18 return array_merge($shared, $this->items);19 }20}2122// Usage23Inertia::share('notifications', ['Welcome back!']);2425return 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;56class UserPermissions implements ProvidesInertiaProperties7{8 public function __construct(#[CurrentUser] protected User $user)9 {10 //11 }1213 public function toInertiaProperties(RenderContext $context): array14 {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;56class UserPermissions implements ProvidesInertiaProperties7{8 public function __construct(#[CurrentUser] protected User $user)9 {10 //11 }1213 public function toInertiaProperties(RenderContext $context): array14 {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);45 // or...67 return Inertia::render('UserProfile')->with($permissions);8}1public function index(UserPermissions $permissions)2{3 return Inertia::render('UserProfile', $permissions);45 // or...67 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 ]);78 // or using method chaining...910 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 ]);78 // or using method chaining...910 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}67inertia::render("Users/Show", ShowProps { user, can_edit: true })1#[derive(Serialize)]2struct ShowProps {3 user: User,4 can_edit: bool,5}67inertia::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.