Skip to content

Conversation

mpociot
Copy link
Contributor

@mpociot mpociot commented May 13, 2025

This PR adds the ability to specify custom dot notated keys to use when deep merging data with Inertia.

For example, let's say you have a component that renders a user pagination via infinite loading using the WhenVisible component:

Route::get('/users', function () {
    return Inertia::render('Users/Index', [
        'results' => Inertia::deepMerge(User::paginate()),
    ]);
});

This works great while scrolling through the site as new users get appended, however when you perform any additional request that results in a redirect to the /users route, the current deepMerge strategy will append the users to the state.

This PR aims to fix this by allowing you to specify on which key(s) the data should be merged. In the case of a pagination, you would return:

Route::get('/users', function () {
    return Inertia::render('Users/Index', [
        'results' => Inertia::deepMerge(User::paginate(), 'data.id'), // <--
    ]);
});

This tells Inertia to try and merge the results.data array by looking at the id property of the underlying objects (if it exists).
If the property is the same, the target object will be replaced with the new one from the response - otherwise it will be appended.

The JS implementation can be found here: inertiajs/inertia#2344

@pascalbaljet pascalbaljet changed the title Allow deepMerge on custom properties [2.x] Allow deepMerge on custom properties Jun 6, 2025
@pascalbaljet pascalbaljet merged commit fe6659d into inertiajs:2.x Jun 13, 2025
18 checks passed
@pascalbaljet
Copy link
Member

Thanks, @mpociot!

@pascalbaljet
Copy link
Member

Follow-up PR #747

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants