Skip to content

Merging Props

Data & Props

Merging Props

Inertia overwrites props with the same name when reloading a page. However, you may need to merge new data with existing data instead. For example, when implementing a "load more" button for paginated results. The Infinite scroll component uses prop merging under the hood.

Prop merging only works during partial reloads. Full page visits will always replace props entirely, even if you've marked them for merging.

Merge Methods

To merge a prop instead of overwriting it, you may use Inertia::merge()inertia::merge() when returning your response.

1Route::get('/items', function () {
2 // Static array of tags...
3 $allTags = [
4 'Laravel', 'React', 'Vue', 'Tailwind', 'Inertia',
5 'PHP', 'JavaScript', 'TypeScript', 'Docker', 'Vite',
6 ];
7 
8 // Get chunk of tags by page...
9 $page = request()->input('page', 1);
10 $perPage = 5;
11 $offset = ($page - 1) * $perPage;
12 $tags = array_slice($allTags, $offset, $perPage);
13 
14 return Inertia::render('Tags/Index', [
15 'tags' => Inertia::merge($tags),
16 ]);
17});
1Route::get('/items', function () {
2 // Static array of tags...
3 $allTags = [
4 'Laravel', 'React', 'Vue', 'Tailwind', 'Inertia',
5 'PHP', 'JavaScript', 'TypeScript', 'Docker', 'Vite',
6 ];
7 
8 // Get chunk of tags by page...
9 $page = request()->input('page', 1);
10 $perPage = 5;
11 $offset = ($page - 1) * $perPage;
12 $tags = array_slice($allTags, $offset, $perPage);
13 
14 return Inertia::render('Tags/Index', [
15 'tags' => Inertia::merge($tags),
16 ]);
17});
1use laravel::inertia::{self, Page, props};
2use laravel::pagination;
3 
4pub async fn index() -> Page {
5 let all_tags = [
6 "Laravel", "React", "Vue", "Tailwind", "Inertia",
7 "Rust", "JavaScript", "TypeScript", "Docker", "Vite",
8 ];
9 
10 let page = pagination::current_page() as usize;
11 let per_page = 5;
12 let offset = (page - 1) * per_page;
13 let tags = all_tags.get(offset..(offset + per_page).min(all_tags.len())).unwrap_or_default();
14 
15 inertia::render("Tags/Index", props! {
16 tags => inertia::merge(tags),
17 })
18}
1use laravel::inertia::{self, Page, props};
2use laravel::pagination;
3 
4pub async fn index() -> Page {
5 let all_tags = [
6 "Laravel", "React", "Vue", "Tailwind", "Inertia",
7 "Rust", "JavaScript", "TypeScript", "Docker", "Vite",
8 ];
9 
10 let page = pagination::current_page() as usize;
11 let per_page = 5;
12 let offset = (page - 1) * per_page;
13 let tags = all_tags.get(offset..(offset + per_page).min(all_tags.len())).unwrap_or_default();
14 
15 inertia::render("Tags/Index", props! {
16 tags => inertia::merge(tags),
17 })
18}

By default, merging appends new items to existing arrays at the root level. You may change this behavior to prepend items instead.

1// Append at root level (default)...
2Inertia::merge($items);
3 
4// Prepend at root level...
5Inertia::merge($items)->prepend();
1// Append at root level (default)...
2Inertia::merge($items);
3 
4// Prepend at root level...
5Inertia::merge($items)->prepend();
1// Append at root level (default)...
2inertia::merge(items);
3 
4// Prepend at root level...
5inertia::merge(items).prepend();
1// Append at root level (default)...
2inertia::merge(items);
3 
4// Prepend at root level...
5inertia::merge(items).prepend();

For more precise control, you can target specific nested properties for merging while replacing the rest of the object by passing the property path to append() or prepend()append_at() or prepend_at().

1// Only append to the 'data' array, replace everything else...
2Inertia::merge(User::paginate())->append('data');
3 
4// Prepend to the 'messages' array...
5Inertia::merge($chatData)->prepend('messages');
1// Only append to the 'data' array, replace everything else...
2Inertia::merge(User::paginate())->append('data');
3 
4// Prepend to the 'messages' array...
5Inertia::merge($chatData)->prepend('messages');
1// Only append to the 'data' array, replace everything else...
2inertia::merge(User::query().order_by(User::id).paginate(15).await?).append_at("data");
3 
4// Prepend to the 'messages' array...
5inertia::merge(chat_data).prepend_at("messages");
1// Only append to the 'data' array, replace everything else...
2inertia::merge(User::query().order_by(User::id).paginate(15).await?).append_at("data");
3 
4// Prepend to the 'messages' array...
5inertia::merge(chat_data).prepend_at("messages");

You can combine multiple operations and target several properties at once.

1Inertia::merge($forumData)
2 ->append('posts')
3 ->prepend('announcements');
4 
5// Target multiple properties...
6Inertia::merge($dashboardData)
7 ->append(['notifications', 'activities']);
1Inertia::merge($forumData)
2 ->append('posts')
3 ->prepend('announcements');
4 
5// Target multiple properties...
6Inertia::merge($dashboardData)
7 ->append(['notifications', 'activities']);
1inertia::merge(forum_data)
2 .append_at("posts")
3 .prepend_at("announcements");
4 
5// Target multiple properties...
6inertia::merge(dashboard_data)
7 .append_at("notifications")
8 .append_at("activities");
1inertia::merge(forum_data)
2 .append_at("posts")
3 .prepend_at("announcements");
4 
5// Target multiple properties...
6inertia::merge(dashboard_data)
7 .append_at("notifications")
8 .append_at("activities");

On the client side, Inertia handles all the merging automatically according to your server-side configuration.

Matching Items

When merging arrays, you may match existing items by a specific field and update them instead of appending duplicates by passing matchOn to append()chaining .match_on(...) with the nested path to the key.

1// Match posts by ID, update existing ones...
2Inertia::merge($postData)->append('data', matchOn: 'id');
3 
4// Multiple properties with different match fields...
5Inertia::merge($complexData)->append([
6 'users.data' => 'id',
7 'messages' => 'uuid',
8]);
1// Match posts by ID, update existing ones...
2Inertia::merge($postData)->append('data', matchOn: 'id');
3 
4// Multiple properties with different match fields...
5Inertia::merge($complexData)->append([
6 'users.data' => 'id',
7 'messages' => 'uuid',
8]);
1// Match posts by ID, update existing ones...
2inertia::merge(post_data)
3 .append_at("data")
4 .match_on("data.id");
5 
6// Multiple properties with different match fields...
7inertia::merge(complex_data)
8 .append_at("users.data")
9 .match_on("users.data.id")
10 .append_at("messages")
11 .match_on("messages.uuid");
1// Match posts by ID, update existing ones...
2inertia::merge(post_data)
3 .append_at("data")
4 .match_on("data.id");
5 
6// Multiple properties with different match fields...
7inertia::merge(complex_data)
8 .append_at("users.data")
9 .match_on("users.data.id")
10 .append_at("messages")
11 .match_on("messages.uuid");

In the first example, Inertia will iterate over the 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.

Deep Merge

Instead of specifying which nested paths should be merged, you may use Inertia::deepMerge()inertia::deep_merge() to ensure a deep merge of the entire structure.

1Route::get('/chat', function () {
2 $chatData = [
3 'messages' => [
4 ['id' => 4, 'text' => 'Hello there!', 'user' => 'Alice'],
5 ['id' => 5, 'text' => 'How are you?', 'user' => 'Bob'],
6 ],
7 'online' => 12,
8 ];
9 
10 return Inertia::render('Chat', [
11 'chat' => Inertia::deepMerge($chatData)->matchOn('messages.id'),
12 ]);
13});
1Route::get('/chat', function () {
2 $chatData = [
3 'messages' => [
4 ['id' => 4, 'text' => 'Hello there!', 'user' => 'Alice'],
5 ['id' => 5, 'text' => 'How are you?', 'user' => 'Bob'],
6 ],
7 'online' => 12,
8 ];
9 
10 return Inertia::render('Chat', [
11 'chat' => Inertia::deepMerge($chatData)->matchOn('messages.id'),
12 ]);
13});
1use laravel::inertia::{self, Page, props};
2use serde_json::json;
3 
4pub async fn index() -> Page {
5 let chat_data = json!({
6 "messages": [
7 { "id": 4, "text": "Hello there!", "user": "Alice" },
8 { "id": 5, "text": "How are you?", "user": "Bob" },
9 ],
10 "online": 12,
11 });
12 
13 inertia::render("Chat", props! {
14 chat => inertia::deep_merge(chat_data).match_on("messages.id"),
15 })
16}
1use laravel::inertia::{self, Page, props};
2use serde_json::json;
3 
4pub async fn index() -> Page {
5 let chat_data = json!({
6 "messages": [
7 { "id": 4, "text": "Hello there!", "user": "Alice" },
8 { "id": 5, "text": "How are you?", "user": "Bob" },
9 ],
10 "online": 12,
11 });
12 
13 inertia::render("Chat", props! {
14 chat => inertia::deep_merge(chat_data).match_on("messages.id"),
15 })
16}

Deep merging was introduced before merge() supported prepending and targeting nested paths. In most cases, merge() with its append and prepend methods should be sufficient.

Client-Side Visits

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.

Combining with Deferred Props

You may combine deferred props with mergeable props to defer the loading of the prop and ultimately mark it as mergeable once it's loaded.

1Route::get('/users', function () {
2 $page = request()->input('page', 1);
3 $perPage = request()->input('per_page', 10);
4 
5 return Inertia::render('Users/Index', [
6 'results' => Inertia::defer(fn() => User::paginate($perPage, page: $page))->deepMerge(),
7 ]);
8});
1Route::get('/users', function () {
2 $page = request()->input('page', 1);
3 $perPage = request()->input('per_page', 10);
4 
5 return Inertia::render('Users/Index', [
6 'results' => Inertia::defer(fn() => User::paginate($perPage, page: $page))->deepMerge(),
7 ]);
8});
1use laravel::inertia::{self, Page, props};
2 
3use crate::models::User;
4 
5pub async fn index() -> Page {
6 inertia::render("Users/Index", props! {
7 results => inertia::defer(async || {
8 User::query().order_by(User::id).paginate(10).await
9 }).deep_merge(),
10 })
11}
1use laravel::inertia::{self, Page, props};
2 
3use crate::models::User;
4 
5pub async fn index() -> Page {
6 inertia::render("Users/Index", props! {
7 results => inertia::defer(async || {
8 User::query().order_by(User::id).paginate(10).await
9 }).deep_merge(),
10 })
11}

Combining with Once Props

You may chain the once() modifier onto a merge prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.

1return Inertia::render('Users/Index', [
2 'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
3]);
1return Inertia::render('Users/Index', [
2 'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
3]);
1inertia::render("Users/Index", props! {
2 activity => inertia::lazy(async move || user.recent_activity().await)
3 .merge()
4 .once(),
5})
1inertia::render("Users/Index", props! {
2 activity => inertia::lazy(async move || user.recent_activity().await)
3 .merge()
4 .once(),
5})

For more information on once props, see the once props documentation.

Resetting Props

On the client side, you can indicate to the server that you would like to reset the prop. This is useful when you want to clear the prop value before merging new data, such as when the user enters a new search query on a paginated list.

The reset request option accepts an array of the prop keys you would like to reset.

1router.reload({
2 reset: ["results"],
3 // ...
4});
1router.reload({
2 reset: ["results"],
3 // ...
4});