From 0298b69a48707012ca6e1050cb44690b7caa6a4d Mon Sep 17 00:00:00 2001
From: Pascal Baljet
By default, Inertia overwrites props with the same name when reloading a page. However, there are instances,
- such as pagination or infinite scrolling, where that is not the desired behavior. In these cases, you can merge
- props instead of overwriting them.
+ such as pagination, where that is not the desired behavior. In these cases, you can merge props
+ instead of overwriting them.
+
+ Prop merging only works during partial reloads. Full page visits will always
+ replace props entirely, even if you've marked them for merging.
From 45b090fdbfdcfaba2d0529cfe4e00afe69ffb468 Mon Sep 17 00:00:00 2001
From: Pascal Baljet
- Use Merging props
Server side
merge
when merging simple arrays, and deepMerge
when working with nested objects
- that contain arrays or complex structures, such as pagination objects.
+ Use merge
when you want to merge at the root level or specific properties, and{' '}
+ deepMerge
when you want to recursively merge nested objects that contain arrays.
matchOn
method to determine how existing items should be matched and updated.
matchOn
to specify multiple keys for matching.
+
+
+ For more precise control over merging, you can use the append()
and prepend()
methods.
+ By default, Inertia::merge()
appends at the root level. You can change this behavior or target
+ specific nested properties.
+
+ You can also use prepend()
to add items to the beginning of arrays, useful for chat messages
+ or activity feeds where newer items appear first.
+
+ Both methods can be chained together and support the matchOn
parameter for item matching.
+
+ You can pass arrays to target multiple properties at once. +
+
+ For advanced scenarios, pass a key-value array where the key is the property path and the value
+ is the matchOn
field.
+
On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will
From fef0fbb660a0e0c2eb67e1155a9d17b977ea08c2 Mon Sep 17 00:00:00 2001
From: Pascal Baljet
By default, Inertia overwrites props with the same name when reloading a page. However, there are instances,
- such as pagination, where that is not the desired behavior. In these cases, you can merge props
- instead of overwriting them.
+ such as pagination, where that is not the desired behavior. In these cases, you can merge props instead of
+ overwriting them.
Prop merging only works during partial reloads. Full page visits will always
@@ -28,141 +27,123 @@ export default function () {
- To specify that a prop should be merged, you can use the
- Use
- During the merging process, if the value is an array, the incoming items will be{' '}
- appended to the existing array by default, not merged by index. You can change this behavior
- and gain more precise control using the methods described in the{' '}
- fine-grained control section. You may also chain the{' '}
-
- In this example, Inertia will iterate over the
- For more precise control over merging, you can use the
- You can also use
- Both methods can be chained together and support the
- You can pass arrays to target multiple properties at once.
- You can combine multiple operations and target several properties at once.
- For advanced scenarios, pass a key-value array where the key is the property path and the value
- is the
+ In this example, Inertia will iterate over the arrays and attempt to match each item by the specified field. If
+ a match is found, the existing item will be replaced. If no match is found, the new item will be appended or
+ prepended as configured.
+
+ Instead of specifying which nested paths should be merged, you can use
- On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will
- append the response to the current prop value. If it's an object, it will merge the response with the current
- prop value. If you have opted to
From 1dcc19d4f1a1e682c661d1cb8cd2b2be9520b211 Mon Sep 17 00:00:00 2001
From: Pascal Baljet
- By default, Inertia overwrites props with the same name when reloading a page. However, there are instances,
- such as pagination, where that is not the desired behavior. In these cases, you can merge props instead of
- overwriting them.
+ Inertia overwrites props with the same name when reloading a page. However, you may need to merge new data with
+ existing data instead of replacing it entirely. For example, when implementing a "load more" button for
+ paginated results. In these cases, you can merge props instead of overwriting them.
Prop merging only works during partial reloads. Full page visits will always
@@ -27,7 +27,8 @@ export default function () {
- To specify that a prop should be merged, you can use the
- By default,
- For more precise control, you can target specific nested properties while leaving the rest of the object
- unchanged.
+ For more precise control, you can target specific nested properties for merging while replacing the rest of the
+ object.
- When merging arrays, you can use the
- In this example, Inertia will iterate over the arrays and attempt to match each item by the specified field. If
- a match is found, the existing item will be replaced. If no match is found, the new item will be appended or
- prepended as configured.
+ In the first example, Inertia will iterate over the
- Instead of specifying which nested paths should be merged, you can use
- On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will merge
- the response with the current prop value according to your server-side configuration (append, prepend, or target
- specific paths). If it's an object, it will merge the response with the current prop value. If you have opted to{' '}
-
From 409fd9a6a30dbf18b0f840e0cdbba7ff32233c3e Mon Sep 17 00:00:00 2001
From: Pascal Baljet
Instead of specifying which nested paths should be merged, you may use
On the client side, Inertia will detect that the prop should be merged and handle the merging process
From fb23760c85b645dc17b58475ab886b981c46c4b5 Mon Sep 17 00:00:00 2001
From: Pascal Baljet
Inertia overwrites props with the same name when reloading a page. However, you may need to merge new data with
- existing data instead of replacing it entirely. For example, when implementing a "load more" button for
- paginated results. In these cases, you can merge props instead of overwriting them.
+ existing data instead. For example, when implementing a "load more" button for paginated results.
Prop merging only works during partial reloads. Full page visits will always
@@ -34,13 +33,13 @@ export default function () {
language="php"
children={dedent`
Route::get('/items', function () {
- // Static array of tags
+ // Static array of tags...
$allTags = [
'Laravel', 'React', 'Vue', 'Tailwind', 'Inertia',
'PHP', 'JavaScript', 'TypeScript', 'Docker', 'Vite',
];
- // Get chunk of tags by page
+ // Get chunk of tags by page...
$page = request()->input('page', 1);
$perPage = 5;
$offset = ($page - 1) * $perPage;
@@ -59,10 +58,10 @@ export default function () {
When merging arrays, you may use the
From 4607f4491bda5fc2b55dd7f224323fab48be58e9 Mon Sep 17 00:00:00 2001
From: Pascal Baljet
To merge a prop instead of overwriting it, you may use the
+ On the client side, Inertia handles all the merging automatically according to your server-side configuration.
+
When merging arrays, you may use the
Instead of specifying which nested paths should be merged, you may use
- On the client side, Inertia will detect that the prop should be merged and handle the merging process
- automatically. If the prop returns an array, it will merge the response with the current prop value according to
- your server-side configuration (append, prepend, target specific paths, or deep merge). If you have used{' '}
-
You can also combine deferred props with mergeable props to defer the loading of
From 020af23de883c6a8ff903412ff8cf250137df70a Mon Sep 17 00:00:00 2001
From: Pascal Baljet
+ You can also merge props directly on the client side without making a server request using{' '}
+ client side visits. Inertia provides{' '}
+ prop helper methods that allow you to append, prepend, or replace prop
+ values.
+
You can also combine deferred props with mergeable props to defer the loading of
Merging props
Server side
Inertia::merge()
or Inertia::deepMerge()
methods on
- the prop value.
- merge
when you want to merge at the root level or specific properties, and{' '}
- deepMerge
when you want to recursively merge nested objects that contain arrays.
- matchOn
method to determine how existing items should be matched and updated.
+ To specify that a prop should be merged, you can use the Inertia::merge()
method on the prop value.
users.data
array and attempt to{' '}
- match each item by its id
field. If a match is found, the existing item will be replaced.
- If no match is found, the new item will be appended.
- matchOn
to specify multiple keys for matching.
- Fine-grained control
- append()
and prepend()
methods.
- By default, Inertia::merge()
appends at the root level. You can change this behavior or target
- specific nested properties.
+ By default, Inertia::merge()
appends new items to existing arrays at the root level. You can change
+ this to prepend items instead.
prepend()
to add items to the beginning of arrays, useful for chat messages
- or activity feeds where newer items appear first.
- matchOn
parameter for item matching.
+ For more precise control, you can target specific nested properties while leaving the rest of the object
+ unchanged.
matchOn
field.
+ When merging arrays, you can use the matchOn
parameter to determine how existing items should be
+ matched and updated instead of simply appending new ones.
matchOn
to specify multiple keys for matching.
+ Inertia::deepMerge()
+ to recursively merge all nested objects that contain arrays.
+ Client side
deepMerge
, Inertia ensures a deep merge of the entire structure.
+ On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will merge
+ the response with the current prop value according to your server-side configuration (append, prepend, or target
+ specific paths). If it's an object, it will merge the response with the current prop value. If you have opted to{' '}
+ deepMerge
, Inertia ensures a deep merge of the entire structure.
Combining with deferred props
Merging props
Server side
Inertia::merge()
method on the prop value.
+ To merge a prop instead of overwriting it, you may use the Inertia::merge()
method when returning
+ your response.
Inertia::merge()
appends new items to existing arrays at the root level. You can change
- this to prepend items instead.
+ The Inertia::merge()
method will append new items to existing arrays at the root level. You may
+ change this behavior to prepend items instead.
Matching items
matchOn
parameter to determine how existing items should be
- matched and updated instead of simply appending new ones.
+ When merging arrays, you may use the matchOn
parameter to match existing items by a specific field
+ and update them instead of simply appending new ones.
data
array and attempt to match each item by
+ its id
field. If a match is found, the existing item will be replaced. If no match is found, the
+ new item will be appended as configured.
matchOn
to specify multiple keys for matching.
- Deep merging
Inertia::deepMerge()
+ Instead of specifying which nested paths should be merged, you may use Inertia::deepMerge()
to recursively merge all nested objects that contain arrays.
Client side
deepMerge
, Inertia ensures a deep merge of the entire structure.
+ On the client side, Inertia will detect that the prop should be merged and handle the merging process
+ automatically. If the prop returns an array, it will merge the response with the current prop value according to
+ your server-side configuration (append, prepend, target specific paths, or deep merge). If you have used{' '}
+ deepMerge
, Inertia will perform a deep merge of the entire structure.
Combining with deferred props
Deep merging
Inertia::deepMerge()
- to recursively merge all nested objects that contain arrays.
+ to ensure a deep merge of the entire structure.
Inertia::deepMerge()
was introduced before Inertia::merge()
had support for prepending
+ and targeting nested paths. In most cases, Inertia::merge()
with its append and prepend methods
+ should be sufficient.
+ Client side
Merging props
Matching items
matchOn
parameter to match existing items by a specific field
- and update them instead of simply appending new ones.
+ and update them instead of appending new ones.
data
array and attempt to match each item by
its id
field. If a match is found, the existing item will be replaced. If no match is found, the
- new item will be appended as configured.
+ new item will be appended.
Deep merging
Server side
+ Merge methods
Inertia::merge()
method when returning
your response.
@@ -91,7 +92,10 @@ export default function () {
Inertia::merge($dashboardData)->append(['notifications', 'activities']);
`}
/>
- Matching items
+ Matching items
matchOn
parameter to match existing items by a specific field
and update them instead of appending new ones.
@@ -114,7 +118,7 @@ export default function () {
its id
field. If a match is found, the existing item will be replaced. If no match is found, the
new item will be appended.
Deep merging
+ Deep merge
Inertia::deepMerge()
to ensure a deep merge of the entire structure.
@@ -142,13 +146,6 @@ export default function () {
and targeting nested paths. In most cases, Inertia::merge()
with its append and prepend methods
should be sufficient.
- Client side
- deepMerge
, Inertia will perform a deep merge of the entire structure.
- Combining with deferred props
Inertia::merge()
with its append and prepend methods
should be sufficient.
+ Client side visits
+ Combining with deferred props