Skip to content

[2.x] Allow deepMerge on custom properties #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from

Conversation

mpociot
Copy link

@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
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