@@ -5,7 +5,7 @@ export const meta = {
5
5
title : 'Responses' ,
6
6
links : [
7
7
{ url : '#creating-responses' , name : 'Creating responses' } ,
8
- { url : '#props ' , name : 'Props ' } ,
8
+ { url : '#properties ' , name : 'Properties ' } ,
9
9
{ url : '#provides-inertia-property' , name : 'ProvidesInertiaProperty interface' } ,
10
10
{ url : '#provides-inertia-properties' , name : 'ProvidesInertiaProperties interface' } ,
11
11
{ url : '#root-template-data' , name : 'Root template data' } ,
@@ -21,10 +21,10 @@ export default function () {
21
21
< P >
22
22
Creating an Inertia response is simple. To get started, invoke the < Code > Inertia::render()</ Code > method within
23
23
your controller or route, providing both the name of the < A href = "/pages" > JavaScript page component</ A > that you
24
- wish to render, as well as any props (data) for the page.
24
+ wish to render, as well as any properties (data) for the page.
25
25
</ P >
26
26
< P >
27
- In the example below, we will pass a single prop (< Code > event</ Code > ) which contains four attributes (
27
+ In the example below, we will pass a single property (< Code > event</ Code > ) which contains four attributes (
28
28
< Code > id</ Code > , < Code > title</ Code > , < Code > start_date</ Code > and < Code > description</ Code > ) to the{ ' ' }
29
29
< Code > Event/Show</ Code > page component.
30
30
</ P >
@@ -69,9 +69,9 @@ export default function () {
69
69
To ensure that pages load quickly, only return the minimum data required for the page. Also, be aware that all
70
70
data returned from the controllers will be visible client-side, so be sure to omit sensitive information.
71
71
</ Notice >
72
- < H2 > Props </ H2 >
72
+ < H2 > Properties </ H2 >
73
73
< P >
74
- To pass data from the server to your page components, you can use props . You can pass various types of values as
74
+ To pass data from the server to your page components, you can use properties . You can pass various types of values as
75
75
props, including primitive types, arrays, objects, and several Laravel-specific types that are automatically resolved:
76
76
</ P >
77
77
< TabbedCode
@@ -116,12 +116,12 @@ export default function () {
116
116
< H2 > ProvidesInertiaProperty interface</ H2 >
117
117
< P >
118
118
When passing props to your components, you may want to create custom classes that can transform themselves into the
119
- appropriate data format. You can do this by implementing the < Code > ProvidesInertiaProperty</ Code > interface.
119
+ appropriate data format. While Laravel's < Code > Arrayable</ Code > interface simply converts objects to arrays, Inertia
120
+ offers the more powerful < Code > ProvidesInertiaProperty</ Code > interface for context-aware transformations.
120
121
</ P >
121
122
< P >
122
- This interface requires you to implement a < Code > toInertiaProperty</ Code > method that returns the transformed value.
123
- The method receives a < Code > PropertyContext</ Code > object which provides access to the property key, current props,
124
- and the request instance.
123
+ This interface requires a < Code > toInertiaProperty</ Code > method that receives a < Code > PropertyContext</ Code > object
124
+ containing the property key, current props, and request instance.
125
125
</ P >
126
126
< TabbedCode
127
127
examples = { [
@@ -165,34 +165,67 @@ export default function () {
165
165
] }
166
166
/>
167
167
< P >
168
- The < Code > avatar</ Code > prop will contain the full avatar URL.
169
- </ P >
170
- < P >
171
- This is particularly useful when you need to create reusable prop transformations or when you need access to the
172
- property key or other props during transformation.
168
+ The < Code > PropertyContext</ Code > gives you access to the property key, which enables powerful patterns like merging
169
+ with shared data:
173
170
</ P >
171
+ < TabbedCode
172
+ examples = { [
173
+ {
174
+ name : 'Laravel' ,
175
+ language : 'php' ,
176
+ code : dedent `
177
+ use Inertia\\Inertia;
178
+ use Inertia\\PropertyContext;
179
+ use Inertia\\ProvidesInertiaProperty;
180
+
181
+ class MergeWithShared implements ProvidesInertiaProperty
182
+ {
183
+ public function __construct(protected array $items = []) {}
184
+
185
+ public function toInertiaProperty(PropertyContext $prop): mixed
186
+ {
187
+ // Access the property key to get shared data
188
+ $shared = Inertia::getShared($prop->key, []);
189
+
190
+ // Merge with the new items
191
+ return array_merge($shared, $this->items);
192
+ }
193
+ }
194
+
195
+ // Usage
196
+ Inertia::share('notifications', ['Welcome back!']);
197
+
198
+ return Inertia::render('Dashboard', [
199
+ 'notifications' => new MergeWithShared(['New message received']),
200
+ // Result: ['Welcome back!', 'New message received']
201
+ ]);
202
+ ` ,
203
+ } ,
204
+ ] }
205
+ />
174
206
< H2 > ProvidesInertiaProperties interface</ H2 >
175
207
< P >
176
208
In some situations you may want to group related props together for reusability across different pages. You can
177
209
accomplish this by implementing the < Code > ProvidesInertiaProperties</ Code > interface.
178
210
</ P >
179
211
< P >
180
- This interface requires you to implement a < Code > toInertiaProperties</ Code > method that returns an array of
181
- key-value pairs. The method receives a < Code > RenderContext</ Code > object which contains the request instance
182
- and component name.
212
+ This interface requires a < Code > toInertiaProperties</ Code > method that returns an array of key-value pairs. The
213
+ method receives a < Code > RenderContext</ Code > object containing the request and component name.
183
214
</ P >
184
215
< TabbedCode
185
216
examples = { [
186
217
{
187
218
name : 'Laravel' ,
188
219
language : 'php' ,
189
220
code : dedent `
221
+ use App\\Models\\User;
222
+ use Illuminate\\Container\\Attributes\\CurrentUser;
190
223
use Inertia\\RenderContext;
191
224
use Inertia\\ProvidesInertiaProperties;
192
225
193
226
class UserPermissions implements ProvidesInertiaProperties
194
227
{
195
- public function __construct(protected User $user) {}
228
+ public function __construct(#[CurrentUser] protected User $user) {}
196
229
197
230
public function toInertiaProperties(RenderContext $context): array
198
231
{
@@ -276,7 +309,7 @@ export default function () {
276
309
] }
277
310
/>
278
311
< P >
279
- Sometimes you may even want to provide data to the root template that will not be sent to your JavaScript page /
312
+ Sometimes you may even want to provide data to the root template that will not be sent to your JavaScript page
280
313
component. This can be accomplished by invoking the < Code > withViewData</ Code > method.
281
314
</ P >
282
315
< TabbedCode
0 commit comments